home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / fileutil.zip / REGEX.C < prev    next >
C/C++ Source or Header  |  1990-09-02  |  86KB  |  2,768 lines

  1. /* Extended regular expression matching and search library.
  2.    Copyright (C) 1985, 1989-90 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18.  
  19. /* To test, compile with -Dtest.  This Dtestable feature turns this into
  20.    a self-contained program which reads a pattern, describes how it
  21.    compiles, then reads a string and searches for it.
  22.    
  23.    On the other hand, if you compile with both -Dtest and -Dcanned you
  24.    can run some tests we've already thought of.  */
  25.  
  26.  
  27. #ifdef emacs
  28.  
  29. /* The `emacs' switch turns on certain special matching commands
  30.   that make sense only in emacs. */
  31.  
  32. #include "config.h"
  33. #include "lisp.h"
  34. #include "buffer.h"
  35. #include "syntax.h"
  36.  
  37. #else  /* not emacs */
  38.  
  39. #if defined (USG) || defined (STDC_HEADERS)
  40. #ifndef BSTRING
  41. #include <string.h>
  42. #define bcopy(s,d,n)    memcpy((d),(s),(n))
  43. #define bcmp(s1,s2,n)    memcmp((s1),(s2),(n))
  44. #define bzero(s,n)    memset((s),0,(n))
  45. #endif
  46. #endif
  47.  
  48. #ifdef STDC_HEADERS
  49. #include <stdlib.h>
  50. #else
  51. char *malloc ();
  52. char *realloc ();
  53. #endif
  54.  
  55. /* Make alloca work the best possible way.  */
  56. #ifdef __GNUC__
  57. #define alloca __builtin_alloca
  58. #else
  59. #ifdef sparc
  60. #include <alloca.h>
  61. #else
  62. char *alloca ();
  63. #endif
  64. #endif
  65.  
  66.  
  67. /* Define the syntax stuff, so we can do the \<, \>, etc.  */
  68.  
  69. /* This must be nonzero for the wordchar and notwordchar pattern
  70.    commands in re_match_2.  */
  71. #ifndef Sword 
  72. #define Sword 1
  73. #endif
  74.  
  75. #define SYNTAX(c) re_syntax_table[c]
  76.  
  77.  
  78. #ifdef SYNTAX_TABLE
  79.  
  80. char *re_syntax_table;
  81.  
  82. #else /* not SYNTAX_TABLE */
  83.  
  84. static char re_syntax_table[256];
  85.  
  86.  
  87. static void
  88. init_syntax_once ()
  89. {
  90.    register int c;
  91.    static int done = 0;
  92.  
  93.    if (done)
  94.      return;
  95.  
  96.    bzero (re_syntax_table, sizeof re_syntax_table);
  97.  
  98.    for (c = 'a'; c <= 'z'; c++)
  99.      re_syntax_table[c] = Sword;
  100.  
  101.    for (c = 'A'; c <= 'Z'; c++)
  102.      re_syntax_table[c] = Sword;
  103.  
  104.    for (c = '0'; c <= '9'; c++)
  105.      re_syntax_table[c] = Sword;
  106.  
  107.    done = 1;
  108. }
  109.  
  110. #endif /* SYNTAX_TABLE */
  111. #endif /* emacs */
  112.  
  113. /* We write fatal error messages on standard error.  */
  114. #include <stdio.h>
  115.  
  116. /* isalpha(3) etc. are used for the character classes.  */
  117. #include <ctype.h>
  118. /* Sequents are missing isgraph.  */
  119. #ifndef isgraph
  120. #define isgraph(c) (isprint((c)) && !isspace((c)))
  121. #endif
  122.  
  123. /* Get the interface, including the syntax bits.  */
  124. #include "regex.h"
  125.  
  126.  
  127. /* These are the command codes that appear in compiled regular
  128.    expressions, one per byte.  Some command codes are followed by
  129.    argument bytes.  A command code can specify any interpretation
  130.    whatsoever for its arguments.  Zero-bytes may appear in the compiled
  131.    regular expression.
  132.    
  133.    The value of `exactn' is needed in search.c (search_buffer) in emacs.
  134.    So regex.h defines a symbol `RE_EXACTN_VALUE' to be 1; the value of
  135.    `exactn' we use here must also be 1.  */
  136.  
  137. enum regexpcode
  138.   {
  139.     unused=0,
  140.     exactn=1, /* Followed by one byte giving n, then by n literal bytes.  */
  141.     begline,  /* Fail unless at beginning of line.  */
  142.     endline,  /* Fail unless at end of line.  */
  143.     jump,     /* Followed by two bytes giving relative address to jump to.  */
  144.     on_failure_jump,     /* Followed by two bytes giving relative address of 
  145.                 place to resume at in case of failure.  */
  146.     finalize_jump,     /* Throw away latest failure point and then jump to 
  147.                 address.  */
  148.     maybe_finalize_jump, /* Like jump but finalize if safe to do so.
  149.                 This is used to jump back to the beginning
  150.                 of a repeat.  If the command that follows
  151.                 this jump is clearly incompatible with the
  152.                 one at the beginning of the repeat, such that
  153.                 we can be sure that there is no use backtracking
  154.                 out of repetitions already completed,
  155.                 then we finalize.  */
  156.     dummy_failure_jump,  /* Jump, and push a dummy failure point. This 
  157.                 failure point will be thrown away if an attempt 
  158.                             is made to use it for a failure. A + construct 
  159.                             makes this before the first repeat.  Also
  160.                             use it as an intermediary kind of jump when
  161.                             compiling an or construct.  */
  162.     succeed_n,     /* Used like on_failure_jump except has to succeed n times;
  163.             then gets turned into an on_failure_jump. The relative
  164.                     address following it is useless until then.  The
  165.                     address is followed by two bytes containing n.  */
  166.     jump_n,     /* Similar to jump, but jump n times only; also the relative
  167.             address following is in turn followed by yet two more bytes
  168.                     containing n.  */
  169.     set_number_at,    /* Set the following relative location to the
  170.                subsequent number.  */
  171.     anychar,     /* Matches any (more or less) one character.  */
  172.     charset,     /* Matches any one char belonging to specified set.
  173.             First following byte is number of bitmap bytes.
  174.             Then come bytes for a bitmap saying which chars are in.
  175.             Bits in each byte are ordered low-bit-first.
  176.             A character is in the set if its bit is 1.
  177.             A character too large to have a bit in the map
  178.             is automatically not in the set.  */
  179.     charset_not, /* Same parameters as charset, but match any character
  180.                     that is not one of those specified.  */
  181.     start_memory, /* Start remembering the text that is matched, for
  182.             storing in a memory register.  Followed by one
  183.                     byte containing the register number.  Register numbers
  184.                     must be in the range 0 through RE_NREGS.  */
  185.     stop_memory, /* Stop remembering the text that is matched
  186.             and store it in a memory register.  Followed by
  187.                     one byte containing the register number. Register
  188.                     numbers must be in the range 0 through RE_NREGS.  */
  189.     duplicate,   /* Match a duplicate of something remembered.
  190.             Followed by one byte containing the index of the memory 
  191.                     register.  */
  192.     before_dot,     /* Succeeds if before point.  */
  193.     at_dot,     /* Succeeds if at point.  */
  194.     after_dot,     /* Succeeds if after point.  */
  195.     begbuf,      /* Succeeds if at beginning of buffer.  */
  196.     endbuf,      /* Succeeds if at end of buffer.  */
  197.     wordchar,    /* Matches any word-constituent character.  */
  198.     notwordchar, /* Matches any char that is not a word-constituent.  */
  199.     wordbeg,     /* Succeeds if at word beginning.  */
  200.     wordend,     /* Succeeds if at word end.  */
  201.     wordbound,   /* Succeeds if at a word boundary.  */
  202.     notwordbound,/* Succeeds if not at a word boundary.  */
  203.     syntaxspec,  /* Matches any character whose syntax is specified.
  204.             followed by a byte which contains a syntax code,
  205.                     e.g., Sword.  */
  206.     notsyntaxspec /* Matches any character whose syntax differs from
  207.                      that specified.  */
  208.   };
  209.  
  210.  
  211. /* Number of failure points to allocate space for initially,
  212.    when matching.  If this number is exceeded, more space is allocated,
  213.    so it is not a hard limit.  */
  214.  
  215. #ifndef NFAILURES
  216. #define NFAILURES 80
  217. #endif
  218.  
  219. #ifndef SIGN_EXTEND_CHAR
  220. #define SIGN_EXTEND_CHAR(x) (x)
  221. #endif
  222.  
  223.  
  224. /* Store NUMBER in two contiguous bytes starting at DESTINATION.  */
  225. #define STORE_NUMBER(destination, number)                \
  226.   { (destination)[0] = (number) & 0377;                    \
  227.     (destination)[1] = (number) >> 8; }
  228.   
  229. /* Same as STORE_NUMBER, except increment the destination pointer to
  230.    the byte after where the number is stored.  Watch out that values for
  231.    DESTINATION such as p + 1 won't work, whereas p will.  */
  232. #define STORE_NUMBER_AND_INCR(destination, number)            \
  233.   { STORE_NUMBER(destination, number);                    \
  234.     (destination) += 2; }
  235.  
  236.  
  237. /* Put into DESTINATION a number stored in two contingous bytes starting
  238.    at SOURCE.  */
  239. #define EXTRACT_NUMBER(destination, source)                \
  240.   { (destination) = *(source) & 0377;                    \
  241.     (destination) += SIGN_EXTEND_CHAR (*(char *)((source) + 1)) << 8; }
  242.  
  243. /* Same as EXTRACT_NUMBER, except increment the pointer for source to
  244.    point to second byte of SOURCE.  Note that SOURCE has to be a value
  245.    such as p, not, e.g., p + 1. */
  246. #define EXTRACT_NUMBER_AND_INCR(destination, source)            \
  247.   { EXTRACT_NUMBER (destination, source);                \
  248.     (source) += 2; }
  249.  
  250.  
  251. /* Specify the precise syntax of regexps for compilation.  This provides
  252.    for compatibility for various utilities which historically have
  253.    different, incompatible syntaxes.
  254.    
  255.    The argument SYNTAX is a bit-mask comprised of the various bits
  256.    defined in regex.h.  */
  257.  
  258. int
  259. re_set_syntax (syntax)
  260.   int syntax;
  261. {
  262.   int ret;
  263.  
  264.   ret = obscure_syntax;
  265.   obscure_syntax = syntax;
  266.   return ret;
  267. }
  268.  
  269. /* Set by re_set_syntax to the current regexp syntax to recognize.  */
  270. int obscure_syntax = 0;
  271.  
  272.  
  273.  
  274. /* Macros for re_compile_pattern, which is found below these definitions.  */
  275.  
  276. #define CHAR_CLASS_MAX_LENGTH  6
  277.  
  278. /* Fetch the next character in the uncompiled pattern, translating it if
  279.    necessary.  */
  280. #define PATFETCH(c)                            \
  281.   {if (p == pend) goto end_of_pattern;                    \
  282.   c = * (unsigned char *) p++;                        \
  283.   if (translate) c = translate[c]; }
  284.  
  285. /* Fetch the next character in the uncompiled pattern, with no
  286.    translation.  */
  287. #define PATFETCH_RAW(c)                            \
  288.  {if (p == pend) goto end_of_pattern;                    \
  289.   c = * (unsigned char *) p++; }
  290.  
  291. #define PATUNFETCH p--
  292.  
  293.  
  294. /* If the buffer isn't allocated when it comes in, use this.  */
  295. #define INIT_BUF_SIZE  28
  296.  
  297. /* Make sure we have at least N more bytes of space in buffer.  */
  298. #define GET_BUFFER_SPACE(n)                        \
  299.   {                                        \
  300.     while (b - bufp->buffer + (n) >= bufp->allocated)            \
  301.       EXTEND_BUFFER;                            \
  302.   }
  303.  
  304. /* Make sure we have one more byte of buffer space and then add CH to it.  */
  305. #define BUFPUSH(ch)                            \
  306.   {                                    \
  307.     GET_BUFFER_SPACE (1);                        \
  308.     *b++ = (char) (ch);                            \
  309.   }
  310.   
  311. /* Extend the buffer by twice its current size via reallociation and
  312.    reset the pointers that pointed into the old allocation to point to
  313.    the correct places in the new allocation.  If extending the buffer
  314.    results in it being larger than 1 << 16, then flag memory exhausted.  */
  315. #define EXTEND_BUFFER                            \
  316.   { char *old_buffer = bufp->buffer;                    \
  317.     if (bufp->allocated == (1L<<16)) goto too_big;            \
  318.     bufp->allocated *= 2;                        \
  319.     if (bufp->allocated > (1L<<16)) bufp->allocated = (1L<<16);        \
  320.     bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated);    \
  321.     if (bufp->buffer == 0)                        \
  322.       goto memory_exhausted;                        \
  323.     b = (b - old_buffer) + bufp->buffer;                \
  324.     if (fixup_jump)                            \
  325.       fixup_jump = (fixup_jump - old_buffer) + bufp->buffer;        \
  326.     if (laststart)                            \
  327.       laststart = (laststart - old_buffer) + bufp->buffer;        \
  328.     begalt = (begalt - old_buffer) + bufp->buffer;            \
  329.     if (pending_exact)                            \
  330.       pending_exact = (pending_exact - old_buffer) + bufp->buffer;    \
  331.   }
  332.  
  333. /* Set the bit for character C in a character set list.  */
  334. #define SET_LIST_BIT(c)  (b[(c) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
  335.  
  336. /* Get the next unsigned number in the uncompiled pattern.  */
  337. #define GET_UNSIGNED_NUMBER(num)                     \
  338.   { if (p != pend)                             \
  339.       {                                 \
  340.         PATFETCH (c);                             \
  341.     while (isdigit (c))                         \
  342.       {                                 \
  343.         if (num < 0)                         \
  344.            num = 0;                         \
  345.             num = num * 10 + c - '0';                     \
  346.         if (p == pend)                         \
  347.            break;                             \
  348.         PATFETCH (c);                         \
  349.       }                                 \
  350.         }                                 \
  351.   }
  352.  
  353. /* Subroutines for re_compile_pattern.  */
  354. static void store_jump (), insert_jump (), store_jump_n (),
  355.         insert_jump_n (), insert_op_2 ();
  356.  
  357.  
  358. /* re_compile_pattern takes a regular-expression string
  359.    and converts it into a buffer full of byte commands for matching.
  360.  
  361.    PATTERN   is the address of the pattern string
  362.    SIZE      is the length of it.
  363.    BUFP        is a  struct re_pattern_buffer *  which points to the info
  364.          on where to store the byte commands.
  365.          This structure contains a  char *  which points to the
  366.          actual space, which should have been obtained with malloc.
  367.          re_compile_pattern may use realloc to grow the buffer space.
  368.  
  369.    The number of bytes of commands can be found out by looking in
  370.    the `struct re_pattern_buffer' that bufp pointed to, after
  371.    re_compile_pattern returns. */
  372.  
  373. char *
  374. re_compile_pattern (pattern, size, bufp)
  375.      char *pattern;
  376.      int size;
  377.      struct re_pattern_buffer *bufp;
  378. {
  379.   register char *b = bufp->buffer;
  380.   register char *p = pattern;
  381.   char *pend = pattern + size;
  382.   register unsigned c, c1;
  383.   char *p1;
  384.   unsigned char *translate = (unsigned char *) bufp->translate;
  385.  
  386.   /* Address of the count-byte of the most recently inserted `exactn'
  387.      command.  This makes it possible to tell whether a new exact-match
  388.      character can be added to that command or requires a new `exactn'
  389.      command.  */
  390.      
  391.   char *pending_exact = 0;
  392.  
  393.   /* Address of the place where a forward-jump should go to the end of
  394.      the containing expression.  Each alternative of an `or', except the
  395.      last, ends with a forward-jump of this sort.  */
  396.  
  397.   char *fixup_jump = 0;
  398.  
  399.   /* Address of start of the most recently finished expression.
  400.      This tells postfix * where to find the start of its operand.  */
  401.  
  402.   char *laststart = 0;
  403.  
  404.   /* In processing a repeat, 1 means zero matches is allowed.  */
  405.  
  406.   char zero_times_ok;
  407.  
  408.   /* In processing a repeat, 1 means many matches is allowed.  */
  409.  
  410.   char many_times_ok;
  411.  
  412.   /* Address of beginning of regexp, or inside of last \(.  */
  413.  
  414.   char *begalt = b;
  415.  
  416.   /* In processing an interval, at least this many matches must be made.  */
  417.   int lower_bound;
  418.  
  419.   /* In processing an interval, at most this many matches can be made.  */
  420.   int upper_bound;
  421.  
  422.   /* Place in pattern (i.e., the {) to which to go back if the interval
  423.      is invalid.  */
  424.   char *beg_interval = 0;
  425.   
  426.   /* Stack of information saved by \( and restored by \).
  427.      Four stack elements are pushed by each \(:
  428.        First, the value of b.
  429.        Second, the value of fixup_jump.
  430.        Third, the value of regnum.
  431.        Fourth, the value of begalt.  */
  432.  
  433.   int stackb[40];
  434.   int *stackp = stackb;
  435.   int *stacke = stackb + 40;
  436.   int *stackt;
  437.  
  438.   /* Counts \('s as they are encountered.  Remembered for the matching \),
  439.      where it becomes the register number to put in the stop_memory
  440.      command.  */
  441.  
  442.   int regnum = 1;
  443.  
  444.   bufp->fastmap_accurate = 0;
  445.  
  446. #ifndef emacs
  447. #ifndef SYNTAX_TABLE
  448.   /* Initialize the syntax table.  */
  449.    init_syntax_once();
  450. #endif
  451. #endif
  452.  
  453.   if (bufp->allocated == 0)
  454.     {
  455.       bufp->allocated = INIT_BUF_SIZE;
  456.       if (bufp->buffer)
  457.     /* EXTEND_BUFFER loses when bufp->allocated is 0.  */
  458.     bufp->buffer = (char *) realloc (bufp->buffer, INIT_BUF_SIZE);
  459.       else
  460.     /* Caller did not allocate a buffer.  Do it for them.  */
  461.     bufp->buffer = (char *) malloc (INIT_BUF_SIZE);
  462.       if (!bufp->buffer) goto memory_exhausted;
  463.       begalt = b = bufp->buffer;
  464.     }
  465.  
  466.   while (p != pend)
  467.     {
  468.       PATFETCH (c);
  469.  
  470.       switch (c)
  471.     {
  472.     case '$':
  473.       {
  474.         char *p1 = p;
  475.         /* When testing what follows the $,
  476.            look past the \-constructs that don't consume anything.  */
  477.         if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  478.           while (p1 != pend)
  479.         {
  480.           if (*p1 == '\\' && p1 + 1 != pend
  481.               && (p1[1] == '<' || p1[1] == '>'
  482.               || p1[1] == '`' || p1[1] == '\''
  483. #ifdef emacs
  484.               || p1[1] == '='
  485. #endif
  486.               || p1[1] == 'b' || p1[1] == 'B'))
  487.             p1 += 2;
  488.           else
  489.             break;
  490.         }
  491.             if (obscure_syntax & RE_TIGHT_VBAR)
  492.           {
  493.         if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS) && p1 != pend)
  494.           goto normal_char;
  495.         /* Make operand of last vbar end before this `$'.  */
  496.         if (fixup_jump)
  497.           store_jump (fixup_jump, jump, b);
  498.         fixup_jump = 0;
  499.         BUFPUSH (endline);
  500.         break;
  501.           }
  502.         /* $ means succeed if at end of line, but only in special contexts.
  503.           If validly in the middle of a pattern, it is a normal character. */
  504.  
  505.             if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS) && p1 != pend)
  506.           goto invalid_pattern;
  507.         if (p1 == pend || *p1 == '\n'
  508.         || (obscure_syntax & RE_CONTEXT_INDEP_OPS)
  509.         || (obscure_syntax & RE_NO_BK_PARENS
  510.             ? *p1 == ')'
  511.             : *p1 == '\\' && p1[1] == ')')
  512.         || (obscure_syntax & RE_NO_BK_VBAR
  513.             ? *p1 == '|'
  514.             : *p1 == '\\' && p1[1] == '|'))
  515.           {
  516.         BUFPUSH (endline);
  517.         break;
  518.           }
  519.         goto normal_char;
  520.           }
  521.     case '^':
  522.       /* ^ means succeed if at beg of line, but only if no preceding 
  523.              pattern.  */
  524.              
  525.           if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS) && laststart)
  526.             goto invalid_pattern;
  527.           if (laststart && p - 2 >= pattern && p[-2] != '\n'
  528.            && !(obscure_syntax & RE_CONTEXT_INDEP_OPS))
  529.         goto normal_char;
  530.       if (obscure_syntax & RE_TIGHT_VBAR)
  531.         {
  532.           if (p != pattern + 1
  533.           && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  534.         goto normal_char;
  535.           BUFPUSH (begline);
  536.           begalt = b;
  537.         }
  538.       else
  539.         BUFPUSH (begline);
  540.       break;
  541.  
  542.     case '+':
  543.     case '?':
  544.       if ((obscure_syntax & RE_BK_PLUS_QM)
  545.           || (obscure_syntax & RE_LIMITED_OPS))
  546.         goto normal_char;
  547.     handle_plus:
  548.     case '*':
  549.       /* If there is no previous pattern, char not special. */
  550.       if (!laststart)
  551.             {
  552.               if (obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  553.                 goto invalid_pattern;
  554.               else if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  555.         goto normal_char;
  556.             }
  557.       /* If there is a sequence of repetition chars,
  558.          collapse it down to just one.  */
  559.       zero_times_ok = 0;
  560.       many_times_ok = 0;
  561.       while (1)
  562.         {
  563.           zero_times_ok |= c != '+';
  564.           many_times_ok |= c != '?';
  565.           if (p == pend)
  566.         break;
  567.           PATFETCH (c);
  568.           if (c == '*')
  569.         ;
  570.           else if (!(obscure_syntax & RE_BK_PLUS_QM)
  571.                && (c == '+' || c == '?'))
  572.         ;
  573.           else if ((obscure_syntax & RE_BK_PLUS_QM)
  574.                && c == '\\')
  575.         {
  576.           int c1;
  577.           PATFETCH (c1);
  578.           if (!(c1 == '+' || c1 == '?'))
  579.             {
  580.               PATUNFETCH;
  581.               PATUNFETCH;
  582.               break;
  583.             }
  584.           c = c1;
  585.         }
  586.           else
  587.         {
  588.           PATUNFETCH;
  589.           break;
  590.         }
  591.         }
  592.  
  593.       /* Star, etc. applied to an empty pattern is equivalent
  594.          to an empty pattern.  */
  595.       if (!laststart)  
  596.         break;
  597.  
  598.       /* Now we know whether or not zero matches is allowed
  599.          and also whether or not two or more matches is allowed.  */
  600.       if (many_times_ok)
  601.         {
  602.           /* If more than one repetition is allowed, put in at the
  603.                  end a backward relative jump from b to before the next
  604.                  jump we're going to put in below (which jumps from
  605.                  laststart to after this jump).  */
  606.               GET_BUFFER_SPACE (3);
  607.           store_jump (b, maybe_finalize_jump, laststart - 3);
  608.           b += 3;      /* Because store_jump put stuff here.  */
  609.         }
  610.           /* On failure, jump from laststart to b + 3, which will be the
  611.              end of the buffer after this jump is inserted.  */
  612.           GET_BUFFER_SPACE (3);
  613.       insert_jump (on_failure_jump, laststart, b + 3, b);
  614.       pending_exact = 0;
  615.       b += 3;
  616.       if (!zero_times_ok)
  617.         {
  618.           /* At least one repetition is required, so insert a
  619.                  dummy-failure before the initial on-failure-jump
  620.                  instruction of the loop. This effects a skip over that
  621.                  instruction the first time we hit that loop.  */
  622.               GET_BUFFER_SPACE (6);
  623.               insert_jump (dummy_failure_jump, laststart, laststart + 6, b);
  624.           b += 3;
  625.         }
  626.       break;
  627.  
  628.     case '.':
  629.       laststart = b;
  630.       BUFPUSH (anychar);
  631.       break;
  632.  
  633.         case '[':
  634.           if (p == pend)
  635.             goto invalid_pattern;
  636.       while (b - bufp->buffer
  637.          > bufp->allocated - 3 - (1 << BYTEWIDTH) / BYTEWIDTH)
  638.         EXTEND_BUFFER;
  639.  
  640.       laststart = b;
  641.       if (*p == '^')
  642.         {
  643.               BUFPUSH (charset_not); 
  644.               p++;
  645.             }
  646.       else
  647.         BUFPUSH (charset);
  648.       p1 = p;
  649.  
  650.       BUFPUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  651.       /* Clear the whole map */
  652.       bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  653.           
  654.       if ((obscure_syntax & RE_HAT_NOT_NEWLINE) && b[-2] == charset_not)
  655.             SET_LIST_BIT ('\n');
  656.  
  657.  
  658.       /* Read in characters and ranges, setting map bits.  */
  659.       while (1)
  660.         {
  661.           PATFETCH (c);
  662.  
  663.           /* If set, \ escapes characters when inside [...].  */
  664.           if ((obscure_syntax & RE_AWK_CLASS_HACK) && c == '\\')
  665.             {
  666.               PATFETCH(c1);
  667.                   SET_LIST_BIT (c1);
  668.               continue;
  669.             }
  670.               if (c == ']')
  671.                 {
  672.                   if (p == p1 + 1)
  673.                     {
  674.               /* If this is an empty bracket expression.  */
  675.                       if ((obscure_syntax & RE_NO_EMPTY_BRACKETS) 
  676.                           && p == pend)
  677.                         goto invalid_pattern;
  678.                     }
  679.                   else 
  680.             /* Stop if this isn't merely a ] inside a bracket
  681.                        expression, but rather the end of a bracket
  682.                        expression.  */
  683.                     break;
  684.                 }
  685.               /* Get a range.  */
  686.               if (p[0] == '-' && p[1] != ']')
  687.         {
  688.                   PATFETCH (c1);
  689.           PATFETCH (c1);
  690.                   
  691.           if ((obscure_syntax & RE_NO_EMPTY_RANGES) && c > c1)
  692.                     goto invalid_pattern;
  693.                     
  694.           if ((obscure_syntax & RE_NO_HYPHEN_RANGE_END) 
  695.                       && c1 == '-' && *p != ']')
  696.                     goto invalid_pattern;
  697.                     
  698.                   while (c <= c1)
  699.             {
  700.                       SET_LIST_BIT (c);
  701.                       c++;
  702.             }
  703.                 }
  704.           else if ((obscure_syntax & RE_CHAR_CLASSES)
  705.             &&  c == '[' && p[0] == ':')
  706.                 {
  707.           /* Longest valid character class word has six characters.  */
  708.                   char str[CHAR_CLASS_MAX_LENGTH];
  709.           PATFETCH (c);
  710.           c1 = 0;
  711.           /* If no ] at end.  */
  712.                   if (p == pend)
  713.                     goto invalid_pattern;
  714.           while (1)
  715.             {
  716.               /* Don't translate the ``character class'' characters.  */
  717.                       PATFETCH_RAW (c);
  718.               if (c == ':' || c == ']' || p == pend
  719.                           || c1 == CHAR_CLASS_MAX_LENGTH)
  720.                 break;
  721.               str[c1++] = c;
  722.             }
  723.           str[c1] = '\0';
  724.           if (p == pend     
  725.               || c == ']'    /* End of the bracket expression.  */
  726.                       || p[0] != ']'
  727.               || p + 1 == pend
  728.                       || (strcmp (str, "alpha") != 0 
  729.                           && strcmp (str, "upper") != 0
  730.               && strcmp (str, "lower") != 0 
  731.                           && strcmp (str, "digit") != 0
  732.               && strcmp (str, "alnum") != 0 
  733.                           && strcmp (str, "xdigit") != 0
  734.               && strcmp (str, "space") != 0 
  735.                           && strcmp (str, "print") != 0
  736.               && strcmp (str, "punct") != 0 
  737.                           && strcmp (str, "graph") != 0
  738.               && strcmp (str, "cntrl") != 0))
  739.             {
  740.                /* Undo the ending character, the letters, and leave 
  741.                           the leading : and [ (but set bits for them).  */
  742.                       c1++;
  743.               while (c1--)    
  744.             PATUNFETCH;
  745.               SET_LIST_BIT ('[');
  746.               SET_LIST_BIT (':');
  747.                 }
  748.                   else
  749.                     {
  750.                       /* The ] at the end of the character class.  */
  751.                       PATFETCH (c);                    
  752.                       if (c != ']')
  753.                         goto invalid_pattern;
  754.               for (c = 0; c < (1 << BYTEWIDTH); c++)
  755.             {
  756.               if ((strcmp (str, "alpha") == 0  && isalpha (c))
  757.                    || (strcmp (str, "upper") == 0  && isupper (c))
  758.                    || (strcmp (str, "lower") == 0  && islower (c))
  759.                    || (strcmp (str, "digit") == 0  && isdigit (c))
  760.                    || (strcmp (str, "alnum") == 0  && isalnum (c))
  761.                    || (strcmp (str, "xdigit") == 0  && isxdigit (c))
  762.                    || (strcmp (str, "space") == 0  && isspace (c))
  763.                    || (strcmp (str, "print") == 0  && isprint (c))
  764.                    || (strcmp (str, "punct") == 0  && ispunct (c))
  765.                    || (strcmp (str, "graph") == 0  && isgraph (c))
  766.                    || (strcmp (str, "cntrl") == 0  && iscntrl (c)))
  767.                 SET_LIST_BIT (c);
  768.             }
  769.             }
  770.                 }
  771.               else
  772.                 SET_LIST_BIT (c);
  773.         }
  774.  
  775.           /* Discard any character set/class bitmap bytes that are all
  776.              0 at the end of the map. Decrement the map-length byte too.  */
  777.           while ((int) b[-1] > 0 && b[b[-1] - 1] == 0) 
  778.             b[-1]--; 
  779.           b += b[-1];
  780.           break;
  781.  
  782.     case '(':
  783.       if (! (obscure_syntax & RE_NO_BK_PARENS))
  784.         goto normal_char;
  785.       else
  786.         goto handle_open;
  787.  
  788.     case ')':
  789.       if (! (obscure_syntax & RE_NO_BK_PARENS))
  790.         goto normal_char;
  791.       else
  792.         goto handle_close;
  793.  
  794.         case '\n':
  795.       if (! (obscure_syntax & RE_NEWLINE_OR))
  796.         goto normal_char;
  797.       else
  798.         goto handle_bar;
  799.  
  800.     case '|':
  801.       if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  802.               && (! laststart  ||  p == pend))
  803.         goto invalid_pattern;
  804.           else if (! (obscure_syntax & RE_NO_BK_VBAR))
  805.         goto normal_char;
  806.       else
  807.         goto handle_bar;
  808.  
  809.     case '{':
  810.            if (! ((obscure_syntax & RE_NO_BK_CURLY_BRACES)
  811.                   && (obscure_syntax & RE_INTERVALS)))
  812.              goto normal_char;
  813.            else
  814.              goto handle_interval;
  815.              
  816.         case '\\':
  817.       if (p == pend) goto invalid_pattern;
  818.       PATFETCH_RAW (c);
  819.       switch (c)
  820.         {
  821.         case '(':
  822.           if (obscure_syntax & RE_NO_BK_PARENS)
  823.         goto normal_backsl;
  824.         handle_open:
  825.           if (stackp == stacke) goto nesting_too_deep;
  826.  
  827.               /* Laststart should point to the start_memory that we are about
  828.                  to push (unless the pattern has RE_NREGS or more ('s).  */
  829.               *stackp++ = b - bufp->buffer;    
  830.           if (regnum < RE_NREGS)
  831.             {
  832.           BUFPUSH (start_memory);
  833.           BUFPUSH (regnum);
  834.             }
  835.           *stackp++ = fixup_jump ? fixup_jump - bufp->buffer + 1 : 0;
  836.           *stackp++ = regnum++;
  837.           *stackp++ = begalt - bufp->buffer;
  838.           fixup_jump = 0;
  839.           laststart = 0;
  840.           begalt = b;
  841.           break;
  842.  
  843.         case ')':
  844.           if (obscure_syntax & RE_NO_BK_PARENS)
  845.         goto normal_backsl;
  846.         handle_close:
  847.           if (stackp == stackb) goto unmatched_close;
  848.           begalt = *--stackp + bufp->buffer;
  849.           if (fixup_jump)
  850.         store_jump (fixup_jump, jump, b);
  851.           if (stackp[-1] < RE_NREGS)
  852.         {
  853.           BUFPUSH (stop_memory);
  854.           BUFPUSH (stackp[-1]);
  855.         }
  856.           stackp -= 2;
  857.               fixup_jump = *stackp ? *stackp + bufp->buffer - 1 : 0;
  858.               laststart = *--stackp + bufp->buffer;
  859.           break;
  860.  
  861.         case '|':
  862.               if ((obscure_syntax & RE_LIMITED_OPS)
  863.               || (obscure_syntax & RE_NO_BK_VBAR))
  864.         goto normal_backsl;
  865.         handle_bar:
  866.               if (obscure_syntax & RE_LIMITED_OPS)
  867.                 goto normal_char;
  868.           /* Insert before the previous alternative a jump which
  869.                  jumps to this alternative if the former fails.  */
  870.               GET_BUFFER_SPACE (6);
  871.               insert_jump (on_failure_jump, begalt, b + 6, b);
  872.           pending_exact = 0;
  873.           b += 3;
  874.           /* The alternative before the previous alternative has a
  875.                  jump after it which gets executed if it gets matched.
  876.                  Adjust that jump so it will jump to the previous
  877.                  alternative's analogous jump (put in below, which in
  878.                  turn will jump to the next (if any) alternative's such
  879.                  jump, etc.).  The last such jump jumps to the correct
  880.                  final destination.  */
  881.               if (fixup_jump)
  882.         store_jump (fixup_jump, jump, b);
  883.                 
  884.           /* Leave space for a jump after previous alternative---to be 
  885.                  filled in later.  */
  886.               fixup_jump = b;
  887.               b += 3;
  888.  
  889.               laststart = 0;
  890.           begalt = b;
  891.           break;
  892.  
  893.             case '{': 
  894.               if (! (obscure_syntax & RE_INTERVALS)
  895.           /* Let \{ be a literal.  */
  896.                   || ((obscure_syntax & RE_INTERVALS)
  897.                       && (obscure_syntax & RE_NO_BK_CURLY_BRACES))
  898.           /* If it's the string "\{".  */
  899.           || (p - 2 == pattern  &&  p == pend))
  900.                 goto normal_backsl;
  901.             handle_interval:
  902.           beg_interval = p - 1;        /* The {.  */
  903.               /* If there is no previous pattern, this isn't an interval.  */
  904.           if (!laststart)
  905.             {
  906.                   if (obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  907.             goto invalid_pattern;
  908.                   else
  909.                     goto normal_backsl;
  910.                 }
  911.               /* It also isn't an interval if not preceded by an re
  912.                  matching a single character or subexpression, or if
  913.                  the current type of intervals can't handle back
  914.                  references and the previous thing is a back reference.  */
  915.               if (! (*laststart == anychar
  916.              || *laststart == charset
  917.              || *laststart == charset_not
  918.              || *laststart == start_memory
  919.              || (*laststart == exactn  &&  laststart[1] == 1)
  920.              || (! (obscure_syntax & RE_NO_BK_REFS)
  921.                          && *laststart == duplicate)))
  922.                 {
  923.                   if (obscure_syntax & RE_NO_BK_CURLY_BRACES)
  924.                     goto normal_char;
  925.                     
  926.           /* Posix extended syntax is handled in previous
  927.                      statement; this is for Posix basic syntax.  */
  928.                   if (obscure_syntax & RE_INTERVALS)
  929.                     goto invalid_pattern;
  930.                     
  931.                   goto normal_backsl;
  932.         }
  933.               lower_bound = -1;            /* So can see if are set.  */
  934.           upper_bound = -1;
  935.               GET_UNSIGNED_NUMBER (lower_bound);
  936.           if (c == ',')
  937.         {
  938.           GET_UNSIGNED_NUMBER (upper_bound);
  939.           if (upper_bound < 0)
  940.             upper_bound = RE_DUP_MAX;
  941.         }
  942.           if (upper_bound < 0)
  943.         upper_bound = lower_bound;
  944.               if (! (obscure_syntax & RE_NO_BK_CURLY_BRACES)) 
  945.                 {
  946.                   if (c != '\\')
  947.                     goto invalid_pattern;
  948.                   PATFETCH (c);
  949.                 }
  950.           if (c != '}' || lower_bound < 0 || upper_bound > RE_DUP_MAX
  951.           || lower_bound > upper_bound 
  952.                   || ((obscure_syntax & RE_NO_BK_CURLY_BRACES) 
  953.               && p != pend  && *p == '{')) 
  954.             {
  955.           if (obscure_syntax & RE_NO_BK_CURLY_BRACES)
  956.                     goto unfetch_interval;
  957.                   else
  958.                     goto invalid_pattern;
  959.         }
  960.  
  961.           /* If upper_bound is zero, don't want to succeed at all; 
  962.           jump from laststart to b + 3, which will be the end of
  963.                  the buffer after this jump is inserted.  */
  964.                  
  965.                if (upper_bound == 0)
  966.                  {
  967.                    GET_BUFFER_SPACE (3);
  968.                    insert_jump (jump, laststart, b + 3, b);
  969.                    b += 3;
  970.                  }
  971.  
  972.                /* Otherwise, after lower_bound number of succeeds, jump
  973.                   to after the jump_n which will be inserted at the end
  974.                   of the buffer, and insert that jump_n.  */
  975.                else 
  976.          { /* Set to 5 if only one repetition is allowed and
  977.                   hence no jump_n is inserted at the current end of
  978.                       the buffer; then only space for the succeed_n is
  979.                       needed.  Otherwise, need space for both the
  980.                       succeed_n and the jump_n.  */
  981.                       
  982.                    unsigned slots_needed = upper_bound == 1 ? 5 : 10;
  983.                      
  984.                    GET_BUFFER_SPACE (slots_needed);
  985.                    /* Initialize the succeed_n to n, even though it will
  986.                       be set by its attendant set_number_at, because
  987.                       re_compile_fastmap will need to know it.  Jump to
  988.                       what the end of buffer will be after inserting
  989.                       this succeed_n and possibly appending a jump_n.  */
  990.                    insert_jump_n (succeed_n, laststart, b + slots_needed, 
  991.                           b, lower_bound);
  992.                    b += 5;     /* Just increment for the succeed_n here.  */
  993.  
  994.           /* More than one repetition is allowed, so put in at
  995.              the end of the buffer a backward jump from b to the
  996.                      succeed_n we put in above.  By the time we've gotten
  997.                      to this jump when matching, we'll have matched once
  998.                      already, so jump back only upper_bound - 1 times.  */
  999.  
  1000.                    if (upper_bound > 1)
  1001.                      {
  1002.                        store_jump_n (b, jump_n, laststart, upper_bound - 1);
  1003.                        b += 5;
  1004.                        /* When hit this when matching, reset the
  1005.                           preceding jump_n's n to upper_bound - 1.  */
  1006.                        BUFPUSH (set_number_at);
  1007.                GET_BUFFER_SPACE (2);
  1008.                        STORE_NUMBER_AND_INCR (b, -5);
  1009.                        STORE_NUMBER_AND_INCR (b, upper_bound - 1);
  1010.                      }
  1011.            /* When hit this when matching, set the succeed_n's n.  */
  1012.                    GET_BUFFER_SPACE (5);
  1013.            insert_op_2 (set_number_at, laststart, b, 5, lower_bound);
  1014.                    b += 5;
  1015.                  }
  1016.               pending_exact = 0;
  1017.           beg_interval = 0;
  1018.               break;
  1019.  
  1020.  
  1021.             unfetch_interval:
  1022.           /* If an invalid interval, match the characters as literals.  */
  1023.            if (beg_interval)
  1024.                  p = beg_interval;
  1025.              else
  1026.                  {
  1027.                    fprintf (stderr, 
  1028.               "regex: no interval beginning to which to backtrack.\n");
  1029.            exit (1);
  1030.                  }
  1031.                  
  1032.                beg_interval = 0;
  1033.                PATFETCH (c);        /* normal_char expects char in `c'.  */
  1034.            goto normal_char;
  1035.            break;
  1036.  
  1037. #ifdef emacs
  1038.         case '=':
  1039.           BUFPUSH (at_dot);
  1040.           break;
  1041.  
  1042.         case 's':    
  1043.           laststart = b;
  1044.           BUFPUSH (syntaxspec);
  1045.           PATFETCH (c);
  1046.           BUFPUSH (syntax_spec_code[c]);
  1047.           break;
  1048.  
  1049.         case 'S':
  1050.           laststart = b;
  1051.           BUFPUSH (notsyntaxspec);
  1052.           PATFETCH (c);
  1053.           BUFPUSH (syntax_spec_code[c]);
  1054.           break;
  1055. #endif /* emacs */
  1056.  
  1057.         case 'w':
  1058.           laststart = b;
  1059.           BUFPUSH (wordchar);
  1060.           break;
  1061.  
  1062.         case 'W':
  1063.           laststart = b;
  1064.           BUFPUSH (notwordchar);
  1065.           break;
  1066.  
  1067.         case '<':
  1068.           BUFPUSH (wordbeg);
  1069.           break;
  1070.  
  1071.         case '>':
  1072.           BUFPUSH (wordend);
  1073.           break;
  1074.  
  1075.         case 'b':
  1076.           BUFPUSH (wordbound);
  1077.           break;
  1078.  
  1079.         case 'B':
  1080.           BUFPUSH (notwordbound);
  1081.           break;
  1082.  
  1083.         case '`':
  1084.           BUFPUSH (begbuf);
  1085.           break;
  1086.  
  1087.         case '\'':
  1088.           BUFPUSH (endbuf);
  1089.           break;
  1090.  
  1091.         case '1':
  1092.         case '2':
  1093.         case '3':
  1094.         case '4':
  1095.         case '5':
  1096.         case '6':
  1097.         case '7':
  1098.         case '8':
  1099.         case '9':
  1100.           if (obscure_syntax & RE_NO_BK_REFS)
  1101.                 goto normal_char;
  1102.               c1 = c - '0';
  1103.           if (c1 >= regnum)
  1104.         {
  1105.             if (obscure_syntax & RE_NO_EMPTY_BK_REF)
  1106.                     goto invalid_pattern;
  1107.                   else
  1108.                     goto normal_char;
  1109.                 }
  1110.               /* Can't back reference to a subexpression if inside of it.  */
  1111.               for (stackt = stackp - 2;  stackt > stackb;  stackt -= 4)
  1112.          if (*stackt == c1)
  1113.           goto normal_char;
  1114.           laststart = b;
  1115.           BUFPUSH (duplicate);
  1116.           BUFPUSH (c1);
  1117.           break;
  1118.  
  1119.         case '+':
  1120.         case '?':
  1121.           if (obscure_syntax & RE_BK_PLUS_QM)
  1122.         goto handle_plus;
  1123.           else
  1124.                 goto normal_backsl;
  1125.               break;
  1126.  
  1127.             default:
  1128.         normal_backsl:
  1129.           /* You might think it would be useful for \ to mean
  1130.          not to translate; but if we don't translate it
  1131.          it will never match anything.  */
  1132.           if (translate) c = translate[c];
  1133.           goto normal_char;
  1134.         }
  1135.       break;
  1136.  
  1137.     default:
  1138.     normal_char:        /* Expects the character in `c'.  */
  1139.       if (!pending_exact || pending_exact + *pending_exact + 1 != b
  1140.           || *pending_exact == 0177 || *p == '*' || *p == '^'
  1141.           || ((obscure_syntax & RE_BK_PLUS_QM)
  1142.           ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  1143.           : (*p == '+' || *p == '?'))
  1144.           || ((obscure_syntax & RE_INTERVALS) 
  1145.                   && ((obscure_syntax & RE_NO_BK_CURLY_BRACES)
  1146.               ? *p == '{'
  1147.                       : (p[0] == '\\' && p[1] == '{'))))
  1148.         {
  1149.           laststart = b;
  1150.           BUFPUSH (exactn);
  1151.           pending_exact = b;
  1152.           BUFPUSH (0);
  1153.         }
  1154.       BUFPUSH (c);
  1155.       (*pending_exact)++;
  1156.     }
  1157.     }
  1158.  
  1159.   if (fixup_jump)
  1160.     store_jump (fixup_jump, jump, b);
  1161.  
  1162.   if (stackp != stackb) goto unmatched_open;
  1163.  
  1164.   bufp->used = b - bufp->buffer;
  1165.   return 0;
  1166.  
  1167.  invalid_pattern:
  1168.   return "Invalid regular expression";
  1169.  
  1170.  unmatched_open:
  1171.   return "Unmatched \\(";
  1172.  
  1173.  unmatched_close:
  1174.   return "Unmatched \\)";
  1175.  
  1176.  end_of_pattern:
  1177.   return "Premature end of regular expression";
  1178.  
  1179.  nesting_too_deep:
  1180.   return "Nesting too deep";
  1181.  
  1182.  too_big:
  1183.   return "Regular expression too big";
  1184.  
  1185.  memory_exhausted:
  1186.   return "Memory exhausted";
  1187. }
  1188.  
  1189.  
  1190. /* Store a jump of the form <OPCODE> <relative address>.
  1191.    Store in the location FROM a jump operation to jump to relative
  1192.    address FROM - TO.  OPCODE is the opcode to store.  */
  1193.  
  1194. static void
  1195. store_jump (from, opcode, to)
  1196.      char *from, *to;
  1197.      char opcode;
  1198. {
  1199.   from[0] = opcode;
  1200.   STORE_NUMBER(from + 1, to - (from + 3));
  1201. }
  1202.  
  1203.  
  1204. /* Open up space before char FROM, and insert there a jump to TO.
  1205.    CURRENT_END gives the end of the storage not in use, so we know 
  1206.    how much data to copy up. OP is the opcode of the jump to insert.
  1207.  
  1208.    If you call this function, you must zero out pending_exact.  */
  1209.  
  1210. static void
  1211. insert_jump (op, from, to, current_end)
  1212.      char op;
  1213.      char *from, *to, *current_end;
  1214. {
  1215.   register char *pfrom = current_end;        /* Copy from here...  */
  1216.   register char *pto = current_end + 3;        /* ...to here.  */
  1217.  
  1218.   while (pfrom != from)                   
  1219.     *--pto = *--pfrom;
  1220.   store_jump (from, op, to);
  1221. }
  1222.  
  1223.  
  1224. /* Store a jump of the form <opcode> <relative address> <n> .
  1225.  
  1226.    Store in the location FROM a jump operation to jump to relative
  1227.    address FROM - TO.  OPCODE is the opcode to store, N is a number the
  1228.    jump uses, say, to decide how many times to jump.
  1229.    
  1230.    If you call this function, you must zero out pending_exact.  */
  1231.  
  1232. static void
  1233. store_jump_n (from, opcode, to, n)
  1234.      char *from, *to;
  1235.      char opcode;
  1236.      unsigned n;
  1237. {
  1238.   from[0] = opcode;
  1239.   STORE_NUMBER (from + 1, to - (from + 3));
  1240.   STORE_NUMBER (from + 3, n);
  1241. }
  1242.  
  1243.  
  1244. /* Similar to insert_jump, but handles a jump which needs an extra
  1245.    number to handle minimum and maximum cases.  Open up space at
  1246.    location FROM, and insert there a jump to TO.  CURRENT_END gives the
  1247.    end of the storage in use, so we know how much data to copy up. OP is
  1248.    the opcode of the jump to insert.
  1249.  
  1250.    If you call this function, you must zero out pending_exact.  */
  1251.  
  1252. static void
  1253. insert_jump_n (op, from, to, current_end, n)
  1254.      char op;
  1255.      char *from, *to, *current_end;
  1256.      unsigned n;
  1257. {
  1258.   register char *pfrom = current_end;        /* Copy from here...  */
  1259.   register char *pto = current_end + 5;        /* ...to here.  */
  1260.  
  1261.   while (pfrom != from)                   
  1262.     *--pto = *--pfrom;
  1263.   store_jump_n (from, op, to, n);
  1264. }
  1265.  
  1266.  
  1267. /* Open up space at location THERE, and insert operation OP followed by
  1268.    NUM_1 and NUM_2.  CURRENT_END gives the end of the storage in use, so
  1269.    we know how much data to copy up.
  1270.  
  1271.    If you call this function, you must zero out pending_exact.  */
  1272.  
  1273. static void
  1274. insert_op_2 (op, there, current_end, num_1, num_2)
  1275.      char op;
  1276.      char *there, *current_end;
  1277.      int num_1, num_2;
  1278. {
  1279.   register char *pfrom = current_end;        /* Copy from here...  */
  1280.   register char *pto = current_end + 5;        /* ...to here.  */
  1281.  
  1282.   while (pfrom != there)                   
  1283.     *--pto = *--pfrom;
  1284.   
  1285.   there[0] = op;
  1286.   STORE_NUMBER (there + 1, num_1);
  1287.   STORE_NUMBER (there + 3, num_2);
  1288. }
  1289.  
  1290.  
  1291.  
  1292. /* Given a pattern, compute a fastmap from it.  The fastmap records
  1293.    which of the (1 << BYTEWIDTH) possible characters can start a string
  1294.    that matches the pattern.  This fastmap is used by re_search to skip
  1295.    quickly over totally implausible text.
  1296.  
  1297.    The caller must supply the address of a (1 << BYTEWIDTH)-byte data 
  1298.    area as bufp->fastmap.
  1299.    The other components of bufp describe the pattern to be used.  */
  1300.  
  1301. void
  1302. re_compile_fastmap (bufp)
  1303.      struct re_pattern_buffer *bufp;
  1304. {
  1305.   unsigned char *pattern = (unsigned char *) bufp->buffer;
  1306.   int size = bufp->used;
  1307.   register char *fastmap = bufp->fastmap;
  1308.   register unsigned char *p = pattern;
  1309.   register unsigned char *pend = pattern + size;
  1310.   register int j, k;
  1311.   unsigned char *translate = (unsigned char *) bufp->translate;
  1312.  
  1313.   unsigned char *stackb[NFAILURES];
  1314.   unsigned char **stackp = stackb;
  1315.  
  1316.   unsigned is_a_succeed_n;
  1317.  
  1318.   bzero (fastmap, (1 << BYTEWIDTH));
  1319.   bufp->fastmap_accurate = 1;
  1320.   bufp->can_be_null = 0;
  1321.       
  1322.   while (p)
  1323.     {
  1324.       is_a_succeed_n = 0;
  1325.       if (p == pend)
  1326.     {
  1327.       bufp->can_be_null = 1;
  1328.       break;
  1329.     }
  1330. #ifdef SWITCH_ENUM_BUG
  1331.       switch ((int) ((enum regexpcode) *p++))
  1332. #else
  1333.       switch ((enum regexpcode) *p++)
  1334. #endif
  1335.     {
  1336.     case exactn:
  1337.       if (translate)
  1338.         fastmap[translate[p[1]]] = 1;
  1339.       else
  1340.         fastmap[p[1]] = 1;
  1341.       break;
  1342.  
  1343.         case begline:
  1344.         case before_dot:
  1345.     case at_dot:
  1346.     case after_dot:
  1347.     case begbuf:
  1348.     case endbuf:
  1349.     case wordbound:
  1350.     case notwordbound:
  1351.     case wordbeg:
  1352.     case wordend:
  1353.           continue;
  1354.  
  1355.     case endline:
  1356.       if (translate)
  1357.         fastmap[translate['\n']] = 1;
  1358.       else
  1359.         fastmap['\n'] = 1;
  1360.             
  1361.       if (bufp->can_be_null != 1)
  1362.         bufp->can_be_null = 2;
  1363.       break;
  1364.  
  1365.     case jump_n:
  1366.         case finalize_jump:
  1367.     case maybe_finalize_jump:
  1368.     case jump:
  1369.     case dummy_failure_jump:
  1370.           EXTRACT_NUMBER_AND_INCR (j, p);
  1371.       p += j;    
  1372.       if (j > 0)
  1373.         continue;
  1374.           /* Jump backward reached implies we just went through
  1375.          the body of a loop and matched nothing.
  1376.          Opcode jumped to should be an on_failure_jump.
  1377.          Just treat it like an ordinary jump.
  1378.          For a * loop, it has pushed its failure point already;
  1379.          If so, discard that as redundant.  */
  1380.  
  1381.           if ((enum regexpcode) *p != on_failure_jump
  1382.           && (enum regexpcode) *p != succeed_n)
  1383.         continue;
  1384.           p++;
  1385.           EXTRACT_NUMBER_AND_INCR (j, p);
  1386.           p += j;        
  1387.           if (stackp != stackb && *stackp == p)
  1388.             stackp--;
  1389.           continue;
  1390.       
  1391.         case on_failure_jump:
  1392.     handle_on_failure_jump:
  1393.           EXTRACT_NUMBER_AND_INCR (j, p);
  1394.           *++stackp = p + j;
  1395.       if (is_a_succeed_n)
  1396.             EXTRACT_NUMBER_AND_INCR (k, p);    /* Skip the n.  */
  1397.       continue;
  1398.  
  1399.     case succeed_n:
  1400.       is_a_succeed_n = 1;
  1401.           /* Get to the number of times to succeed.  */
  1402.           p += 2;        
  1403.       /* Increment p past the n for when k != 0.  */
  1404.           EXTRACT_NUMBER_AND_INCR (k, p);
  1405.           if (k == 0)
  1406.         {
  1407.               p -= 4;
  1408.               goto handle_on_failure_jump;
  1409.             }
  1410.           continue;
  1411.           
  1412.     case set_number_at:
  1413.           p += 4;
  1414.           continue;
  1415.  
  1416.         case start_memory:
  1417.     case stop_memory:
  1418.       p++;
  1419.       continue;
  1420.  
  1421.     case duplicate:
  1422.       bufp->can_be_null = 1;
  1423.       fastmap['\n'] = 1;
  1424.     case anychar:
  1425.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1426.         if (j != '\n')
  1427.           fastmap[j] = 1;
  1428.       if (bufp->can_be_null)
  1429.         return;
  1430.       /* Don't return; check the alternative paths
  1431.          so we can set can_be_null if appropriate.  */
  1432.       break;
  1433.  
  1434.     case wordchar:
  1435.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1436.         if (SYNTAX (j) == Sword)
  1437.           fastmap[j] = 1;
  1438.       break;
  1439.  
  1440.     case notwordchar:
  1441.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1442.         if (SYNTAX (j) != Sword)
  1443.           fastmap[j] = 1;
  1444.       break;
  1445.  
  1446. #ifdef emacs
  1447.     case syntaxspec:
  1448.       k = *p++;
  1449.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1450.         if (SYNTAX (j) == (enum syntaxcode) k)
  1451.           fastmap[j] = 1;
  1452.       break;
  1453.  
  1454.     case notsyntaxspec:
  1455.       k = *p++;
  1456.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1457.         if (SYNTAX (j) != (enum syntaxcode) k)
  1458.           fastmap[j] = 1;
  1459.       break;
  1460. #endif /* not emacs */
  1461.  
  1462.     case charset:
  1463.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  1464.         if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  1465.           {
  1466.         if (translate)
  1467.           fastmap[translate[j]] = 1;
  1468.         else
  1469.           fastmap[j] = 1;
  1470.           }
  1471.       break;
  1472.  
  1473.     case charset_not:
  1474.       /* Chars beyond end of map must be allowed */
  1475.       for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  1476.         if (translate)
  1477.           fastmap[translate[j]] = 1;
  1478.         else
  1479.           fastmap[j] = 1;
  1480.  
  1481.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  1482.         if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  1483.           {
  1484.         if (translate)
  1485.           fastmap[translate[j]] = 1;
  1486.         else
  1487.           fastmap[j] = 1;
  1488.           }
  1489.       break;
  1490.     }
  1491.  
  1492.       /* Get here means we have successfully found the possible starting
  1493.          characters of one path of the pattern.  We need not follow this
  1494.          path any farther.  Instead, look at the next alternative
  1495.          remembered in the stack.  */
  1496.    if (stackp != stackb)
  1497.     p = *stackp--;
  1498.       else
  1499.     break;
  1500.     }
  1501. }
  1502.  
  1503.  
  1504.  
  1505. /* Like re_search_2, below, but only one string is specified, and
  1506.    doesn't let you say where to stop matching. */
  1507.  
  1508. int
  1509. re_search (pbufp, string, size, startpos, range, regs)
  1510.      struct re_pattern_buffer *pbufp;
  1511.      char *string;
  1512.      int size, startpos, range;
  1513.      struct re_registers *regs;
  1514. {
  1515.   return re_search_2 (pbufp, (char *) 0, 0, string, size, startpos, range, 
  1516.               regs, size);
  1517. }
  1518.  
  1519.  
  1520. /* Using the compiled pattern in PBUFP->buffer, first tries to match the
  1521.    virtual concatenation of STRING1 and STRING2, starting first at index
  1522.    STARTPOS, then at STARTPOS + 1, and so on.  RANGE is the number of
  1523.    places to try before giving up.  If RANGE is negative, it searches
  1524.    backwards, i.e., the starting positions tried are STARTPOS, STARTPOS
  1525.    - 1, etc.  STRING1 and STRING2 are of SIZE1 and SIZE2, respectively.
  1526.    In REGS, return the indices of the virtual concatenation of STRING1
  1527.    and STRING2 that matched the entire PBUFP->buffer and its contained
  1528.    subexpressions.  Do not consider matching one past the index MSTOP in
  1529.    the virtual concatenation of STRING1 and STRING2.
  1530.  
  1531.    The value returned is the position in the strings at which the match
  1532.    was found, or -1 if no match was found, or -2 if error (such as
  1533.    failure stack overflow).  */
  1534.  
  1535. int
  1536. re_search_2 (pbufp, string1, size1, string2, size2, startpos, range,
  1537.          regs, mstop)
  1538.      struct re_pattern_buffer *pbufp;
  1539.      char *string1, *string2;
  1540.      int size1, size2;
  1541.      int startpos;
  1542.      register int range;
  1543.      struct re_registers *regs;
  1544.      int mstop;
  1545. {
  1546.   register char *fastmap = pbufp->fastmap;
  1547.   register unsigned char *translate = (unsigned char *) pbufp->translate;
  1548.   int total_size = size1 + size2;
  1549.   int endpos = startpos + range;
  1550.   int val;
  1551.  
  1552.   /* Check for out-of-range starting position.  */
  1553.   if (startpos < 0  ||  startpos > total_size)
  1554.     return -1;
  1555.     
  1556.   /* Fix up range if it would eventually take startpos outside of the
  1557.      virtual concatenation of string1 and string2.  */
  1558.   if (endpos < -1)
  1559.     range = -1 - startpos;
  1560.   else if (endpos > total_size)
  1561.     range = total_size - startpos;
  1562.  
  1563.   /* Update the fastmap now if not correct already.  */
  1564.   if (fastmap && !pbufp->fastmap_accurate)
  1565.     re_compile_fastmap (pbufp);
  1566.   
  1567.   /* If the search isn't to be a backwards one, don't waste time in a
  1568.      long search for a pattern that says it is anchored.  */
  1569.   if (pbufp->used > 0 && (enum regexpcode) pbufp->buffer[0] == begbuf
  1570.       && range > 0)
  1571.     {
  1572.       if (startpos > 0)
  1573.     return -1;
  1574.       else
  1575.     range = 1;
  1576.     }
  1577.  
  1578.   while (1)
  1579.     { 
  1580.       /* If a fastmap is supplied, skip quickly over characters that
  1581.          cannot possibly be the start of a match.  Note, however, that
  1582.          if the pattern can possibly match the null string, we must
  1583.          test it at each starting point so that we take the first null
  1584.          string we get.  */
  1585.  
  1586.       if (fastmap && startpos < total_size && pbufp->can_be_null != 1)
  1587.     {
  1588.       if (range > 0)    /* Searching forwards.  */
  1589.         {
  1590.           register int lim = 0;
  1591.           register unsigned char *p;
  1592.           int irange = range;
  1593.           if (startpos < size1 && startpos + range >= size1)
  1594.         lim = range - (size1 - startpos);
  1595.  
  1596.           p = ((unsigned char *)
  1597.            &(startpos >= size1 ? string2 - size1 : string1)[startpos]);
  1598.  
  1599.               while (range > lim && !fastmap[translate 
  1600.                                              ? translate[*p++]
  1601.                                              : *p++])
  1602.             range--;
  1603.           startpos += irange - range;
  1604.         }
  1605.       else                /* Searching backwards.  */
  1606.         {
  1607.           register unsigned char c;
  1608.  
  1609.               if (string1 == 0 || startpos >= size1)
  1610.         c = string2[startpos - size1];
  1611.           else 
  1612.         c = string1[startpos];
  1613.  
  1614.               c &= 0xff;
  1615.           if (translate ? !fastmap[translate[c]] : !fastmap[c])
  1616.         goto advance;
  1617.         }
  1618.     }
  1619.  
  1620.       if (range >= 0 && startpos == total_size
  1621.       && fastmap && pbufp->can_be_null == 0)
  1622.     return -1;
  1623.  
  1624.       val = re_match_2 (pbufp, string1, size1, string2, size2, startpos,
  1625.             regs, mstop);
  1626.       if (val >= 0)
  1627.     return startpos;
  1628.       if (val == -2)
  1629.     return -2;
  1630.  
  1631. #ifdef C_ALLOCA
  1632.       alloca (0);
  1633. #endif /* C_ALLOCA */
  1634.  
  1635.     advance:
  1636.       if (!range) 
  1637.         break;
  1638.       else if (range > 0) 
  1639.         {
  1640.           range--; 
  1641.           startpos++;
  1642.         }
  1643.       else
  1644.         {
  1645.           range++; 
  1646.           startpos--;
  1647.         }
  1648.     }
  1649.   return -1;
  1650. }
  1651.  
  1652.  
  1653.  
  1654. #ifndef emacs   /* emacs never uses this.  */
  1655. int
  1656. re_match (pbufp, string, size, pos, regs)
  1657.      struct re_pattern_buffer *pbufp;
  1658.      char *string;
  1659.      int size, pos;
  1660.      struct re_registers *regs;
  1661. {
  1662.   return re_match_2 (pbufp, (char *) 0, 0, string, size, pos, regs, size); 
  1663. }
  1664. #endif /* not emacs */
  1665.  
  1666.  
  1667. /* The following are used for re_match_2, defined below:  */
  1668.  
  1669. /* Roughly the maximum number of failure points on the stack.  Would be
  1670.    exactly that if always pushed MAX_NUM_FAILURE_ITEMS each time we failed.  */
  1671.    
  1672. int re_max_failures = 2000;
  1673.  
  1674. /* Routine used by re_match_2.  */
  1675. static int bcmp_translate ();
  1676.  
  1677.  
  1678. /* Structure and accessing macros used in re_match_2:  */
  1679.  
  1680. struct register_info
  1681. {
  1682.   unsigned is_active : 1;
  1683.   unsigned matched_something : 1;
  1684. };
  1685.  
  1686. #define IS_ACTIVE(R)  ((R).is_active)
  1687. #define MATCHED_SOMETHING(R)  ((R).matched_something)
  1688.  
  1689.  
  1690. /* Macros used by re_match_2:  */
  1691.  
  1692.  
  1693. /* I.e., regstart, regend, and reg_info.  */
  1694.  
  1695. #define NUM_REG_ITEMS  3
  1696.  
  1697. /* We push at most this many things on the stack whenever we
  1698.    fail.  The `+ 2' refers to PATTERN_PLACE and STRING_PLACE, which are
  1699.    arguments to the PUSH_FAILURE_POINT macro.  */
  1700.  
  1701. #define MAX_NUM_FAILURE_ITEMS   (RE_NREGS * NUM_REG_ITEMS + 2)
  1702.  
  1703.  
  1704. /* We push this many things on the stack whenever we fail.  */
  1705.  
  1706. #define NUM_FAILURE_ITEMS  (last_used_reg * NUM_REG_ITEMS + 2)
  1707.  
  1708.  
  1709. /* This pushes most of the information about the current state we will want
  1710.    if we ever fail back to it.  */
  1711.  
  1712. #define PUSH_FAILURE_POINT(pattern_place, string_place)            \
  1713.   {                                    \
  1714.     short last_used_reg, this_reg;                    \
  1715.                                     \
  1716.     /* Find out how many registers are active or have been matched.    \
  1717.        (Aside from register zero, which is only set at the end.)  */    \
  1718.     for (last_used_reg = RE_NREGS - 1; last_used_reg > 0; last_used_reg--)\
  1719.       if (regstart[last_used_reg] != (unsigned char *) -1)        \
  1720.         break;                                \
  1721.                                     \
  1722.     if (stacke - stackp < NUM_FAILURE_ITEMS)                \
  1723.       {                                    \
  1724.     unsigned char **stackx;                        \
  1725.     if (stacke - stackb > re_max_failures * MAX_NUM_FAILURE_ITEMS)    \
  1726.       return -2;                            \
  1727.                                     \
  1728.         /* Roughly double the size of the stack.  */            \
  1729.         stackx = (unsigned char **) alloca (2 * MAX_NUM_FAILURE_ITEMS    \
  1730.                             * (stacke - stackb)        \
  1731.                                             * sizeof (unsigned char *));\
  1732.     /* Only copy what is in use.  */                \
  1733.         bcopy (stackb, stackx, (stackp - stackb) * sizeof (char *));    \
  1734.     stackp = stackx + (stackp - stackb);                \
  1735.     stackb = stackx;                        \
  1736.     stacke = stackb + 2 * MAX_NUM_FAILURE_ITEMS * (stacke - stackb);\
  1737.       }                                    \
  1738.                                     \
  1739.     /* Now push the info for each of those registers.  */        \
  1740.     for (this_reg = 1; this_reg <= last_used_reg; this_reg++)        \
  1741.       {                                    \
  1742.         *stackp++ = regstart[this_reg];                    \
  1743.         *stackp++ = regend[this_reg];                    \
  1744.         *stackp++ = (unsigned char *) ®_info[this_reg];        \
  1745.       }                                    \
  1746.                                     \
  1747.     /* Push how many registers we saved.  */                \
  1748.     *stackp++ = (unsigned char *) last_used_reg;            \
  1749.                                     \
  1750.     *stackp++ = pattern_place;                                          \
  1751.     *stackp++ = string_place;                                           \
  1752.   }
  1753.   
  1754.  
  1755. /* This pops what PUSH_FAILURE_POINT pushes.  */
  1756.  
  1757. #define POP_FAILURE_POINT()                        \
  1758.   {                                    \
  1759.     int temp;                                \
  1760.     stackp -= 2;        /* Remove failure points.  */        \
  1761.     temp = (int) *--stackp;    /* How many regs pushed.  */            \
  1762.     temp *= NUM_REG_ITEMS;    /* How much to take off the stack.  */    \
  1763.     stackp -= temp;         /* Remove the register info.  */    \
  1764.   }
  1765.  
  1766.  
  1767. #define MATCHING_IN_FIRST_STRING  (dend == end_match_1)
  1768.  
  1769. /* Is true if there is a first string and if PTR is pointing anywhere
  1770.    inside it or just past the end.  */
  1771.    
  1772. #define IS_IN_FIRST_STRING(ptr)                     \
  1773.     (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
  1774.  
  1775. /* Call before fetching a character with *d.  This switches over to
  1776.    string2 if necessary.  */
  1777.  
  1778. #define PREFETCH                            \
  1779.  while (d == dend)                                \
  1780.   {                                    \
  1781.     /* end of string2 => fail.  */                    \
  1782.     if (dend == end_match_2)                         \
  1783.       goto fail;                            \
  1784.     /* end of string1 => advance to string2.  */             \
  1785.     d = string2;                                \
  1786.     dend = end_match_2;                            \
  1787.   }
  1788.  
  1789.  
  1790. /* Call this when have matched something; it sets `matched' flags for the
  1791.    registers corresponding to the subexpressions of which we currently
  1792.    are inside.  */
  1793. #define SET_REGS_MATCHED                         \
  1794.   { unsigned this_reg;                             \
  1795.     for (this_reg = 0; this_reg < RE_NREGS; this_reg++)         \
  1796.       {                                 \
  1797.         if (IS_ACTIVE(reg_info[this_reg]))                \
  1798.           MATCHED_SOMETHING(reg_info[this_reg]) = 1;            \
  1799.         else                                \
  1800.           MATCHED_SOMETHING(reg_info[this_reg]) = 0;            \
  1801.       }                                 \
  1802.   }
  1803.  
  1804. /* Test if at very beginning or at very end of the virtual concatenation
  1805.    of string1 and string2.  If there is only one string, we've put it in
  1806.    string2.  */
  1807.  
  1808. #define AT_STRINGS_BEG  (d == (size1 ? string1 : string2)  ||  !size2)
  1809. #define AT_STRINGS_END  (d == end2)    
  1810.  
  1811. #define AT_WORD_BOUNDARY                        \
  1812.   (AT_STRINGS_BEG || AT_STRINGS_END || IS_A_LETTER (d - 1) != IS_A_LETTER (d))
  1813.  
  1814. /* We have two special cases to check for: 
  1815.      1) if we're past the end of string1, we have to look at the first
  1816.         character in string2;
  1817.      2) if we're before the beginning of string2, we have to look at the
  1818.         last character in string1; we assume there is a string1, so use
  1819.         this in conjunction with AT_STRINGS_BEG.  */
  1820. #define IS_A_LETTER(d)                            \
  1821.   (SYNTAX ((d) == end1 ? *string2 : (d) == string2 - 1 ? *(end1 - 1) : *(d))\
  1822.    == Sword)
  1823.  
  1824.  
  1825. /* Match the pattern described by PBUFP against the virtual
  1826.    concatenation of STRING1 and STRING2, which are of SIZE1 and SIZE2,
  1827.    respectively.  Start the match at index POS in the virtual
  1828.    concatenation of STRING1 and STRING2.  In REGS, return the indices of
  1829.    the virtual concatenation of STRING1 and STRING2 that matched the
  1830.    entire PBUFP->buffer and its contained subexpressions.  Do not
  1831.    consider matching one past the index MSTOP in the virtual
  1832.    concatenation of STRING1 and STRING2.
  1833.  
  1834.    If pbufp->fastmap is nonzero, then it had better be up to date.
  1835.  
  1836.    The reason that the data to match are specified as two components
  1837.    which are to be regarded as concatenated is so this function can be
  1838.    used directly on the contents of an Emacs buffer.
  1839.  
  1840.    -1 is returned if there is no match.  -2 is returned if there is an
  1841.    error (such as match stack overflow).  Otherwise the value is the
  1842.    length of the substring which was matched.  */
  1843.  
  1844. int
  1845. re_match_2 (pbufp, string1_arg, size1, string2_arg, size2, pos, regs, mstop)
  1846.      struct re_pattern_buffer *pbufp;
  1847.      char *string1_arg, *string2_arg;
  1848.      int size1, size2;
  1849.      int pos;
  1850.      struct re_registers *regs;
  1851.      int mstop;
  1852. {
  1853.   register unsigned char *p = (unsigned char *) pbufp->buffer;
  1854.  
  1855.   /* Pointer to beyond end of buffer.  */
  1856.   register unsigned char *pend = p + pbufp->used;
  1857.  
  1858.   unsigned char *string1 = (unsigned char *) string1_arg;
  1859.   unsigned char *string2 = (unsigned char *) string2_arg;
  1860.   unsigned char *end1;        /* Just past end of first string.  */
  1861.   unsigned char *end2;        /* Just past end of second string.  */
  1862.  
  1863.   /* Pointers into string1 and string2, just past the last characters in
  1864.      each to consider matching.  */
  1865.   unsigned char *end_match_1, *end_match_2;
  1866.  
  1867.   register unsigned char *d, *dend;
  1868.   register int mcnt;            /* Multipurpose.  */
  1869.   unsigned char *translate = (unsigned char *) pbufp->translate;
  1870.   unsigned is_a_jump_n = 0;
  1871.  
  1872.  /* Failure point stack.  Each place that can handle a failure further
  1873.     down the line pushes a failure point on this stack.  It consists of
  1874.     restart, regend, and reg_info for all registers corresponding to the
  1875.     subexpressions we're currently inside, plus the number of such
  1876.     registers, and, finally, two char *'s.  The first char * is where to
  1877.     resume scanning the pattern; the second one is where to resume
  1878.     scanning the strings.  If the latter is zero, the failure point is a
  1879.     ``dummy''; if a failure happens and the failure point is a dummy, it
  1880.     gets discarded and the next next one is tried.  */
  1881.  
  1882.   unsigned char *initial_stack[MAX_NUM_FAILURE_ITEMS * NFAILURES];
  1883.   unsigned char **stackb = initial_stack;
  1884.   unsigned char **stackp = stackb;
  1885.   unsigned char **stacke = &stackb[MAX_NUM_FAILURE_ITEMS * NFAILURES];
  1886.  
  1887.  
  1888.   /* Information on the contents of registers. These are pointers into
  1889.      the input strings; they record just what was matched (on this
  1890.      attempt) by a subexpression part of the pattern, that is, the
  1891.      regnum-th regstart pointer points to where in the pattern we began
  1892.      matching and the regnum-th regend points to right after where we
  1893.      stopped matching the regnum-th subexpression.  (The zeroth register
  1894.      keeps track of what the whole pattern matches.)  */
  1895.      
  1896.   unsigned char *regstart[RE_NREGS];
  1897.   unsigned char *regend[RE_NREGS];
  1898.  
  1899.   /* The is_active field of reg_info helps us keep track of which (possibly
  1900.      nested) subexpressions we are currently in. The matched_something
  1901.      field of reg_info[reg_num] helps us tell whether or not we have
  1902.      matched any of the pattern so far this time through the reg_num-th
  1903.      subexpression.  These two fields get reset each time through any
  1904.      loop their register is in.  */
  1905.  
  1906.   struct register_info reg_info[RE_NREGS];
  1907.  
  1908.  
  1909.   /* The following record the register info as found in the above
  1910.      variables when we find a match better than any we've seen before. 
  1911.      This happens as we backtrack through the failure points, which in
  1912.      turn happens only if we have not yet matched the entire string.  */
  1913.  
  1914.   unsigned best_regs_set = 0;
  1915.   unsigned char *best_regstart[RE_NREGS];
  1916.   unsigned char *best_regend[RE_NREGS];
  1917.  
  1918.  
  1919.   /* Initialize subexpression text positions to -1 to mark ones that no
  1920.      \( or ( and \) or ) has been seen for. Also set all registers to
  1921.      inactive and mark them as not having matched anything or ever
  1922.      failed.  */
  1923.   for (mcnt = 0; mcnt < RE_NREGS; mcnt++)
  1924.     {
  1925.       regstart[mcnt] = regend[mcnt] = (unsigned char *) -1;
  1926.       IS_ACTIVE (reg_info[mcnt]) = 0;
  1927.       MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  1928.     }
  1929.   
  1930.   if (regs)
  1931.     for (mcnt = 0; mcnt < RE_NREGS; mcnt++)
  1932.       regs->start[mcnt] = regs->end[mcnt] = -1;
  1933.  
  1934.   /* Set up pointers to ends of strings.
  1935.      Don't allow the second string to be empty unless both are empty.  */
  1936.   if (size2 == 0)
  1937.     {
  1938.       string2 = string1;
  1939.       size2 = size1;
  1940.       string1 = 0;
  1941.       size1 = 0;
  1942.     }
  1943.   end1 = string1 + size1;
  1944.   end2 = string2 + size2;
  1945.  
  1946.   /* Compute where to stop matching, within the two strings.  */
  1947.   if (mstop <= size1)
  1948.     {
  1949.       end_match_1 = string1 + mstop;
  1950.       end_match_2 = string2;
  1951.     }
  1952.   else
  1953.     {
  1954.       end_match_1 = end1;
  1955.       end_match_2 = string2 + mstop - size1;
  1956.     }
  1957.  
  1958.   /* `p' scans through the pattern as `d' scans through the data. `dend'
  1959.      is the end of the input string that `d' points within. `d' is
  1960.      advanced into the following input string whenever necessary, but
  1961.      this happens before fetching; therefore, at the beginning of the
  1962.      loop, `d' can be pointing at the end of a string, but it cannot
  1963.      equal string2.  */
  1964.  
  1965.   if (size1 != 0 && pos <= size1)
  1966.     d = string1 + pos, dend = end_match_1;
  1967.   else
  1968.     d = string2 + pos - size1, dend = end_match_2;
  1969.  
  1970.  
  1971.   /* This loops over pattern commands.  It exits by returning from the
  1972.      function if match is complete, or it drops through if match fails
  1973.      at this starting point in the input data.  */
  1974.  
  1975.   while (1)
  1976.     {
  1977.       is_a_jump_n = 0;
  1978.       /* End of pattern means we might have succeeded.  */
  1979.       if (p == pend)
  1980.     {
  1981.       /* If not end of string, try backtracking.  Otherwise done.  */
  1982.           if (d != end_match_2)
  1983.         {
  1984.               if (stackp != stackb)
  1985.                 {
  1986.                   /* More failure points to try.  */
  1987.  
  1988.                   unsigned in_same_string = 
  1989.                           IS_IN_FIRST_STRING (best_regend[0]) 
  1990.                         == MATCHING_IN_FIRST_STRING;
  1991.  
  1992.                   /* If exceeds best match so far, save it.  */
  1993.                   if (! best_regs_set
  1994.                       || (in_same_string && d > best_regend[0])
  1995.                       || (! in_same_string && ! MATCHING_IN_FIRST_STRING))
  1996.                     {
  1997.                       best_regs_set = 1;
  1998.                       best_regend[0] = d;    /* Never use regstart[0].  */
  1999.                       
  2000.                       for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
  2001.                         {
  2002.                           best_regstart[mcnt] = regstart[mcnt];
  2003.                           best_regend[mcnt] = regend[mcnt];
  2004.                         }
  2005.                     }
  2006.                   goto fail;           
  2007.                 }
  2008.               /* If no failure points, don't restore garbage.  */
  2009.               else if (best_regs_set)   
  2010.                 {
  2011.           restore_best_regs:
  2012.                   /* Restore best match.  */
  2013.                   d = best_regend[0];
  2014.                   
  2015.           for (mcnt = 0; mcnt < RE_NREGS; mcnt++)
  2016.             {
  2017.               regstart[mcnt] = best_regstart[mcnt];
  2018.               regend[mcnt] = best_regend[mcnt];
  2019.             }
  2020.                 }
  2021.             }
  2022.  
  2023.       /* If caller wants register contents data back, convert it 
  2024.          to indices.  */
  2025.       if (regs)
  2026.         {
  2027.           regs->start[0] = pos;
  2028.           if (MATCHING_IN_FIRST_STRING)
  2029.         regs->end[0] = d - string1;
  2030.           else
  2031.         regs->end[0] = d - string2 + size1;
  2032.           for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
  2033.         {
  2034.           if (regend[mcnt] == (unsigned char *) -1)
  2035.             {
  2036.               regs->start[mcnt] = -1;
  2037.               regs->end[mcnt] = -1;
  2038.               continue;
  2039.             }
  2040.           if (IS_IN_FIRST_STRING (regstart[mcnt]))
  2041.             regs->start[mcnt] = regstart[mcnt] - string1;
  2042.           else
  2043.             regs->start[mcnt] = regstart[mcnt] - string2 + size1;
  2044.                     
  2045.           if (IS_IN_FIRST_STRING (regend[mcnt]))
  2046.             regs->end[mcnt] = regend[mcnt] - string1;
  2047.           else
  2048.             regs->end[mcnt] = regend[mcnt] - string2 + size1;
  2049.         }
  2050.         }
  2051.       return d - pos - (MATCHING_IN_FIRST_STRING 
  2052.                 ? string1 
  2053.                 : string2 - size1);
  2054.         }
  2055.  
  2056.       /* Otherwise match next pattern command.  */
  2057. #ifdef SWITCH_ENUM_BUG
  2058.       switch ((int) ((enum regexpcode) *p++))
  2059. #else
  2060.       switch ((enum regexpcode) *p++)
  2061. #endif
  2062.     {
  2063.  
  2064.     /* \( [or `(', as appropriate] is represented by start_memory,
  2065.            \) by stop_memory.  Both of those commands are followed by
  2066.            a register number in the next byte.  The text matched
  2067.            within the \( and \) is recorded under that number.  */
  2068.     case start_memory:
  2069.           regstart[*p] = d;
  2070.           IS_ACTIVE (reg_info[*p]) = 1;
  2071.           MATCHED_SOMETHING (reg_info[*p]) = 0;
  2072.           p++;
  2073.           break;
  2074.  
  2075.     case stop_memory:
  2076.           regend[*p] = d;
  2077.           IS_ACTIVE (reg_info[*p]) = 0;
  2078.  
  2079.           /* If just failed to match something this time around with a sub-
  2080.          expression that's in a loop, try to force exit from the loop.  */
  2081.           if ((! MATCHED_SOMETHING (reg_info[*p])
  2082.            || (enum regexpcode) p[-3] == start_memory)
  2083.           && (p + 1) != pend)              
  2084.             {
  2085.           register unsigned char *p2 = p + 1;
  2086.               mcnt = 0;
  2087.               switch (*p2++)
  2088.                 {
  2089.                   case jump_n:
  2090.             is_a_jump_n = 1;
  2091.                   case finalize_jump:
  2092.           case maybe_finalize_jump:
  2093.           case jump:
  2094.           case dummy_failure_jump:
  2095.                     EXTRACT_NUMBER_AND_INCR (mcnt, p2);
  2096.             if (is_a_jump_n)
  2097.               p2 += 2;
  2098.                     break;
  2099.                 }
  2100.           p2 += mcnt;
  2101.         
  2102.               /* If the next operation is a jump backwards in the pattern
  2103.              to an on_failure_jump, exit from the loop by forcing a
  2104.                  failure after pushing on the stack the on_failure_jump's 
  2105.                  jump in the pattern, and d.  */
  2106.           if (mcnt < 0 && (enum regexpcode) *p2++ == on_failure_jump)
  2107.         {
  2108.                   EXTRACT_NUMBER_AND_INCR (mcnt, p2);
  2109.                   PUSH_FAILURE_POINT (p2 + mcnt, d);
  2110.                   goto fail;
  2111.                 }
  2112.             }
  2113.           p++;
  2114.           break;
  2115.  
  2116.     /* \<digit> has been turned into a `duplicate' command which is
  2117.            followed by the numeric value of <digit> as the register number.  */
  2118.         case duplicate:
  2119.       {
  2120.         int regno = *p++;   /* Get which register to match against */
  2121.         register unsigned char *d2, *dend2;
  2122.  
  2123.         /* Where in input to try to start matching.  */
  2124.             d2 = regstart[regno];
  2125.             
  2126.             /* Where to stop matching; if both the place to start and
  2127.                the place to stop matching are in the same string, then
  2128.                set to the place to stop, otherwise, for now have to use
  2129.                the end of the first string.  */
  2130.  
  2131.             dend2 = ((IS_IN_FIRST_STRING (regstart[regno]) 
  2132.               == IS_IN_FIRST_STRING (regend[regno]))
  2133.              ? regend[regno] : end_match_1);
  2134.         while (1)
  2135.           {
  2136.         /* If necessary, advance to next segment in register
  2137.                    contents.  */
  2138.         while (d2 == dend2)
  2139.           {
  2140.             if (dend2 == end_match_2) break;
  2141.             if (dend2 == regend[regno]) break;
  2142.             d2 = string2, dend2 = regend[regno];  /* end of string1 => advance to string2. */
  2143.           }
  2144.         /* At end of register contents => success */
  2145.         if (d2 == dend2) break;
  2146.  
  2147.         /* If necessary, advance to next segment in data.  */
  2148.         PREFETCH;
  2149.  
  2150.         /* How many characters left in this segment to match.  */
  2151.         mcnt = dend - d;
  2152.                 
  2153.         /* Want how many consecutive characters we can match in
  2154.                    one shot, so, if necessary, adjust the count.  */
  2155.                 if (mcnt > dend2 - d2)
  2156.           mcnt = dend2 - d2;
  2157.                   
  2158.         /* Compare that many; failure if mismatch, else move
  2159.                    past them.  */
  2160.         if (translate 
  2161.                     ? bcmp_translate (d, d2, mcnt, translate) 
  2162.                     : bcmp (d, d2, mcnt))
  2163.           goto fail;
  2164.         d += mcnt, d2 += mcnt;
  2165.           }
  2166.       }
  2167.       break;
  2168.  
  2169.     case anychar:
  2170.       PREFETCH;      /* Fetch a data character. */
  2171.       /* Match anything but a newline, maybe even a null.  */
  2172.       if ((translate ? translate[*d] : *d) == '\n'
  2173.               || ((obscure_syntax & RE_DOT_NOT_NULL) 
  2174.                   && (translate ? translate[*d] : *d) == '\000'))
  2175.         goto fail;
  2176.       SET_REGS_MATCHED;
  2177.           d++;
  2178.       break;
  2179.  
  2180.     case charset:
  2181.     case charset_not:
  2182.       {
  2183.         int not = 0;        /* Nonzero for charset_not.  */
  2184.         register int c;
  2185.         if (*(p - 1) == (unsigned char) charset_not)
  2186.           not = 1;
  2187.  
  2188.         PREFETCH;        /* Fetch a data character. */
  2189.  
  2190.         if (translate)
  2191.           c = translate[*d];
  2192.         else
  2193.           c = *d;
  2194.  
  2195.         if (c < *p * BYTEWIDTH
  2196.         && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  2197.           not = !not;
  2198.  
  2199.         p += 1 + *p;
  2200.  
  2201.         if (!not) goto fail;
  2202.         SET_REGS_MATCHED;
  2203.             d++;
  2204.         break;
  2205.       }
  2206.  
  2207.     case begline:
  2208.           if ((size1 != 0 && d == string1)
  2209.               || (size1 == 0 && size2 != 0 && d == string2)
  2210.               || (d && d[-1] == '\n')
  2211.               || (size1 == 0 && size2 == 0))
  2212.             break;
  2213.           else
  2214.             goto fail;
  2215.             
  2216.     case endline:
  2217.       if (d == end2
  2218.           || (d == end1 ? (size2 == 0 || *string2 == '\n') : *d == '\n'))
  2219.         break;
  2220.       goto fail;
  2221.  
  2222.     /* `or' constructs are handled by starting each alternative with
  2223.            an on_failure_jump that points to the start of the next
  2224.            alternative.  Each alternative except the last ends with a
  2225.            jump to the joining point.  (Actually, each jump except for
  2226.            the last one really jumps to the following jump, because
  2227.            tensioning the jumps is a hassle.)  */
  2228.  
  2229.     /* The start of a stupid repeat has an on_failure_jump that points
  2230.        past the end of the repeat text. This makes a failure point so 
  2231.            that on failure to match a repetition, matching restarts past
  2232.            as many repetitions have been found with no way to fail and
  2233.            look for another one.  */
  2234.  
  2235.     /* A smart repeat is similar but loops back to the on_failure_jump
  2236.        so that each repetition makes another failure point.  */
  2237.  
  2238.     case on_failure_jump:
  2239.         on_failure:
  2240.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2241.           PUSH_FAILURE_POINT (p + mcnt, d);
  2242.           break;
  2243.  
  2244.     /* The end of a smart repeat has a maybe_finalize_jump back.
  2245.        Change it either to a finalize_jump or an ordinary jump.  */
  2246.     case maybe_finalize_jump:
  2247.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2248.       {
  2249.         register unsigned char *p2 = p;
  2250.         /* Compare what follows with the beginning of the repeat.
  2251.            If we can establish that there is nothing that they would
  2252.            both match, we can change to finalize_jump.  */
  2253.         while (p2 + 1 != pend
  2254.            && (*p2 == (unsigned char) stop_memory
  2255.                || *p2 == (unsigned char) start_memory))
  2256.           p2 += 2;                /* Skip over reg number.  */
  2257.         if (p2 == pend)
  2258.           p[-3] = (unsigned char) finalize_jump;
  2259.         else if (*p2 == (unsigned char) exactn
  2260.              || *p2 == (unsigned char) endline)
  2261.           {
  2262.         register int c = *p2 == (unsigned char) endline ? '\n' : p2[2];
  2263.         register unsigned char *p1 = p + mcnt;
  2264.         /* p1[0] ... p1[2] are an on_failure_jump.
  2265.            Examine what follows that.  */
  2266.         if (p1[3] == (unsigned char) exactn && p1[5] != c)
  2267.           p[-3] = (unsigned char) finalize_jump;
  2268.         else if (p1[3] == (unsigned char) charset
  2269.              || p1[3] == (unsigned char) charset_not)
  2270.           {
  2271.             int not = p1[3] == (unsigned char) charset_not;
  2272.             if (c < p1[4] * BYTEWIDTH
  2273.             && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  2274.               not = !not;
  2275.             /* `not' is 1 if c would match.  */
  2276.             /* That means it is not safe to finalize.  */
  2277.             if (!not)
  2278.               p[-3] = (unsigned char) finalize_jump;
  2279.           }
  2280.           }
  2281.       }
  2282.       p -= 2;        /* Point at relative address again.  */
  2283.       if (p[-1] != (unsigned char) finalize_jump)
  2284.         {
  2285.           p[-1] = (unsigned char) jump;    
  2286.           goto nofinalize;
  2287.         }
  2288.         /* Note fall through.  */
  2289.  
  2290.     /* The end of a stupid repeat has a finalize_jump back to the
  2291.            start, where another failure point will be made which will
  2292.            point to after all the repetitions found so far.  */
  2293.  
  2294.         /* Take off failure points put on by matching on_failure_jump 
  2295.            because didn't fail.  Also remove the register information
  2296.            put on by the on_failure_jump.  */
  2297.         case finalize_jump:
  2298.           POP_FAILURE_POINT ();
  2299.         /* Note fall through.  */
  2300.         
  2301.     /* Jump without taking off any failure points.  */
  2302.         case jump:
  2303.     nofinalize:
  2304.       EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2305.       p += mcnt;
  2306.       break;
  2307.  
  2308.         case dummy_failure_jump:
  2309.           /* Normally, the on_failure_jump pushes a failure point, which
  2310.              then gets popped at finalize_jump.  We will end up at
  2311.              finalize_jump, also, and with a pattern of, say, `a+', we
  2312.              are skipping over the on_failure_jump, so we have to push
  2313.              something meaningless for finalize_jump to pop.  */
  2314.           PUSH_FAILURE_POINT (0, 0);
  2315.           goto nofinalize;
  2316.  
  2317.  
  2318.         /* Have to succeed matching what follows at least n times.  Then
  2319.           just handle like an on_failure_jump.  */
  2320.         case succeed_n: 
  2321.           EXTRACT_NUMBER (mcnt, p + 2);
  2322.           /* Originally, this is how many times we HAVE to succeed.  */
  2323.           if (mcnt)
  2324.             {
  2325.                mcnt--;
  2326.            p += 2;
  2327.                STORE_NUMBER_AND_INCR (p, mcnt);
  2328.             }
  2329.       else if (mcnt == 0)
  2330.             {
  2331.           p[2] = unused;
  2332.               p[3] = unused;
  2333.               goto on_failure;
  2334.             }
  2335.           else
  2336.         { 
  2337.               fprintf (stderr, "regex: the succeed_n's n is not set.\n");
  2338.               exit (1);
  2339.         }
  2340.           break;
  2341.         
  2342.         case jump_n: 
  2343.           EXTRACT_NUMBER (mcnt, p + 2);
  2344.           /* Originally, this is how many times we CAN jump.  */
  2345.           if (mcnt)
  2346.             {
  2347.                mcnt--;
  2348.                STORE_NUMBER(p + 2, mcnt);
  2349.            goto nofinalize;         /* Do the jump without taking off
  2350.                             any failure points.  */
  2351.             }
  2352.           /* If don't have to jump any more, skip over the rest of command.  */
  2353.       else      
  2354.         p += 4;             
  2355.           break;
  2356.         
  2357.     case set_number_at:
  2358.       {
  2359.           register unsigned char *p1;
  2360.  
  2361.             EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2362.             p1 = p + mcnt;
  2363.             EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2364.         STORE_NUMBER (p1, mcnt);
  2365.             break;
  2366.           }
  2367.  
  2368.         /* Ignore these.  Used to ignore the n of succeed_n's which
  2369.            currently have n == 0.  */
  2370.         case unused:
  2371.           break;
  2372.  
  2373.         case wordbound:
  2374.       if (AT_WORD_BOUNDARY)
  2375.         break;
  2376.       goto fail;
  2377.  
  2378.     case notwordbound:
  2379.       if (AT_WORD_BOUNDARY)
  2380.         goto fail;
  2381.       break;
  2382.  
  2383.     case wordbeg:
  2384.       if (IS_A_LETTER (d) && (!IS_A_LETTER (d - 1) || AT_STRINGS_BEG))
  2385.         break;
  2386.       goto fail;
  2387.  
  2388.     case wordend:
  2389.           /* Have to check if AT_STRINGS_BEG before looking at d - 1.  */
  2390.       if (!AT_STRINGS_BEG && IS_A_LETTER (d - 1) 
  2391.               && (!IS_A_LETTER (d) || AT_STRINGS_END))
  2392.         break;
  2393.       goto fail;
  2394.  
  2395. #ifdef emacs
  2396.     case before_dot:
  2397.       if (PTR_CHAR_POS (d) >= point)
  2398.         goto fail;
  2399.       break;
  2400.  
  2401.     case at_dot:
  2402.       if (PTR_CHAR_POS (d) != point)
  2403.         goto fail;
  2404.       break;
  2405.  
  2406.     case after_dot:
  2407.       if (PTR_CHAR_POS (d) <= point)
  2408.         goto fail;
  2409.       break;
  2410.  
  2411.     case wordchar:
  2412.       mcnt = (int) Sword;
  2413.       goto matchsyntax;
  2414.  
  2415.     case syntaxspec:
  2416.       mcnt = *p++;
  2417.     matchsyntax:
  2418.       PREFETCH;
  2419.       if (SYNTAX (*d++) != (enum syntaxcode) mcnt) goto fail;
  2420.           SET_REGS_MATCHED;
  2421.       break;
  2422.       
  2423.     case notwordchar:
  2424.       mcnt = (int) Sword;
  2425.       goto matchnotsyntax;
  2426.  
  2427.     case notsyntaxspec:
  2428.       mcnt = *p++;
  2429.     matchnotsyntax:
  2430.       PREFETCH;
  2431.       if (SYNTAX (*d++) == (enum syntaxcode) mcnt) goto fail;
  2432.       SET_REGS_MATCHED;
  2433.           break;
  2434.  
  2435. #else /* not emacs */
  2436.  
  2437.     case wordchar:
  2438.       PREFETCH;
  2439.           if (!IS_A_LETTER (d))
  2440.             goto fail;
  2441.       SET_REGS_MATCHED;
  2442.       break;
  2443.       
  2444.     case notwordchar:
  2445.       PREFETCH;
  2446.       if (IS_A_LETTER (d))
  2447.             goto fail;
  2448.           SET_REGS_MATCHED;
  2449.       break;
  2450.  
  2451. #endif /* not emacs */
  2452.  
  2453.     case begbuf:
  2454.           if (AT_STRINGS_BEG)
  2455.             break;
  2456.           goto fail;
  2457.  
  2458.         case endbuf:
  2459.       if (AT_STRINGS_END)
  2460.         break;
  2461.       goto fail;
  2462.  
  2463.     case exactn:
  2464.       /* Match the next few pattern characters exactly.
  2465.          mcnt is how many characters to match.  */
  2466.       mcnt = *p++;
  2467.       /* This is written out as an if-else so we don't waste time
  2468.              testing `translate' inside the loop.  */
  2469.           if (translate)
  2470.         {
  2471.           do
  2472.         {
  2473.           PREFETCH;
  2474.           if (translate[*d++] != *p++) goto fail;
  2475.         }
  2476.           while (--mcnt);
  2477.         }
  2478.       else
  2479.         {
  2480.           do
  2481.         {
  2482.           PREFETCH;
  2483.           if (*d++ != *p++) goto fail;
  2484.         }
  2485.           while (--mcnt);
  2486.         }
  2487.       SET_REGS_MATCHED;
  2488.           break;
  2489.     }
  2490.       continue;  /* Successfully executed one pattern command; keep going.  */
  2491.  
  2492.     /* Jump here if any matching operation fails. */
  2493.     fail:
  2494.       if (stackp != stackb)
  2495.     /* A restart point is known.  Restart there and pop it. */
  2496.     {
  2497.           short last_used_reg, this_reg;
  2498.           
  2499.           /* If this failure point is from a dummy_failure_point, just
  2500.              skip it.  */
  2501.       if (!stackp[-2])
  2502.             {
  2503.               POP_FAILURE_POINT ();
  2504.               goto fail;
  2505.             }
  2506.  
  2507.           d = *--stackp;
  2508.       p = *--stackp;
  2509.           if (d >= string1 && d <= end1)
  2510.         dend = end_match_1;
  2511.           /* Restore register info.  */
  2512.           last_used_reg = (short) *--stackp;
  2513.           
  2514.           /* Make the ones that weren't saved -1 or 0 again.  */
  2515.           for (this_reg = RE_NREGS - 1; this_reg > last_used_reg; this_reg--)
  2516.             {
  2517.               regend[this_reg] = (unsigned char *) -1;
  2518.               regstart[this_reg] = (unsigned char *) -1;
  2519.               IS_ACTIVE (reg_info[this_reg]) = 0;
  2520.               MATCHED_SOMETHING (reg_info[this_reg]) = 0;
  2521.             }
  2522.           
  2523.           /* And restore the rest from the stack.  */
  2524.           for ( ; this_reg > 0; this_reg--)
  2525.             {
  2526.               reg_info[this_reg] = *(struct register_info *) *--stackp;
  2527.               regend[this_reg] = *--stackp;
  2528.               regstart[this_reg] = *--stackp;
  2529.             }
  2530.     }
  2531.       else
  2532.         break;   /* Matching at this starting point really fails.  */
  2533.     }
  2534.  
  2535.   if (best_regs_set)
  2536.     goto restore_best_regs;
  2537.   return -1;                     /* Failure to match.  */
  2538. }
  2539.  
  2540.  
  2541. static int
  2542. bcmp_translate (s1, s2, len, translate)
  2543.      unsigned char *s1, *s2;
  2544.      register int len;
  2545.      unsigned char *translate;
  2546. {
  2547.   register unsigned char *p1 = s1, *p2 = s2;
  2548.   while (len)
  2549.     {
  2550.       if (translate [*p1++] != translate [*p2++]) return 1;
  2551.       len--;
  2552.     }
  2553.   return 0;
  2554. }
  2555.  
  2556.  
  2557.  
  2558. /* Entry points compatible with 4.2 BSD regex library.  */
  2559.  
  2560. #ifndef emacs
  2561.  
  2562. static struct re_pattern_buffer re_comp_buf;
  2563.  
  2564. char *
  2565. re_comp (s)
  2566.      char *s;
  2567. {
  2568.   if (!s)
  2569.     {
  2570.       if (!re_comp_buf.buffer)
  2571.     return "No previous regular expression";
  2572.       return 0;
  2573.     }
  2574.  
  2575.   if (!re_comp_buf.buffer)
  2576.     {
  2577.       if (!(re_comp_buf.buffer = (char *) malloc (200)))
  2578.     return "Memory exhausted";
  2579.       re_comp_buf.allocated = 200;
  2580.       if (!(re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH)))
  2581.     return "Memory exhausted";
  2582.     }
  2583.   return re_compile_pattern (s, strlen (s), &re_comp_buf);
  2584. }
  2585.  
  2586. int
  2587. re_exec (s)
  2588.      char *s;
  2589. {
  2590.   int len = strlen (s);
  2591.   return 0 <= re_search (&re_comp_buf, s, len, 0, len,
  2592.              (struct re_registers *) 0);
  2593. }
  2594. #endif /* not emacs */
  2595.  
  2596.  
  2597.  
  2598. #ifdef test
  2599.  
  2600. #include <stdio.h>
  2601.  
  2602. /* Indexed by a character, gives the upper case equivalent of the
  2603.    character.  */
  2604.  
  2605. char upcase[0400] = 
  2606.   { 000, 001, 002, 003, 004, 005, 006, 007,
  2607.     010, 011, 012, 013, 014, 015, 016, 017,
  2608.     020, 021, 022, 023, 024, 025, 026, 027,
  2609.     030, 031, 032, 033, 034, 035, 036, 037,
  2610.     040, 041, 042, 043, 044, 045, 046, 047,
  2611.     050, 051, 052, 053, 054, 055, 056, 057,
  2612.     060, 061, 062, 063, 064, 065, 066, 067,
  2613.     070, 071, 072, 073, 074, 075, 076, 077,
  2614.     0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  2615.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  2616.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  2617.     0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
  2618.     0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  2619.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  2620.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  2621.     0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
  2622.     0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  2623.     0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
  2624.     0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
  2625.     0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
  2626.     0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
  2627.     0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
  2628.     0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  2629.     0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  2630.     0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  2631.     0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
  2632.     0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
  2633.     0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
  2634.     0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
  2635.     0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
  2636.     0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  2637.     0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377
  2638.   };
  2639.  
  2640. #ifdef canned
  2641.  
  2642. #include "tests.h"
  2643.  
  2644. typedef enum { extended_test, basic_test } test_type;
  2645.  
  2646. /* Use this to run the tests we've thought of.  */
  2647.  
  2648. void
  2649. main ()
  2650. {
  2651.   test_type t = extended_test;
  2652.  
  2653.   if (t == basic_test)
  2654.     {
  2655.       printf ("Running basic tests:\n\n");
  2656.       test_posix_basic ();
  2657.     }
  2658.   else if (t == extended_test)
  2659.     {
  2660.       printf ("Running extended tests:\n\n");
  2661.       test_posix_extended (); 
  2662.     }
  2663. }
  2664.  
  2665. #else /* not canned */
  2666.  
  2667. /* Use this to run interactive tests.  */
  2668.  
  2669. void
  2670. main (argc, argv)
  2671.      int argc;
  2672.      char **argv;
  2673. {
  2674.   char pat[80];
  2675.   struct re_pattern_buffer buf;
  2676.   int i;
  2677.   char c;
  2678.   char fastmap[(1 << BYTEWIDTH)];
  2679.  
  2680.   /* Allow a command argument to specify the style of syntax.  */
  2681.   if (argc > 1)
  2682.     obscure_syntax = atoi (argv[1]);
  2683.  
  2684.   buf.allocated = 40;
  2685.   buf.buffer = (char *) malloc (buf.allocated);
  2686.   buf.fastmap = fastmap;
  2687.   buf.translate = upcase;
  2688.  
  2689.   while (1)
  2690.     {
  2691.       gets (pat);
  2692.  
  2693.       if (*pat)
  2694.     {
  2695.           re_compile_pattern (pat, strlen(pat), &buf);
  2696.  
  2697.       for (i = 0; i < buf.used; i++)
  2698.         printchar (buf.buffer[i]);
  2699.  
  2700.       putchar ('\n');
  2701.  
  2702.       printf ("%d allocated, %d used.\n", buf.allocated, buf.used);
  2703.  
  2704.       re_compile_fastmap (&buf);
  2705.       printf ("Allowed by fastmap: ");
  2706.       for (i = 0; i < (1 << BYTEWIDTH); i++)
  2707.         if (fastmap[i]) printchar (i);
  2708.       putchar ('\n');
  2709.     }
  2710.  
  2711.       gets (pat);    /* Now read the string to match against */
  2712.  
  2713.       i = re_match (&buf, pat, strlen (pat), 0, 0);
  2714.       printf ("Match value %d.\n", i);
  2715.     }
  2716. }
  2717.  
  2718. #endif
  2719.  
  2720.  
  2721. #ifdef NOTDEF
  2722. print_buf (bufp)
  2723.      struct re_pattern_buffer *bufp;
  2724. {
  2725.   int i;
  2726.  
  2727.   printf ("buf is :\n----------------\n");
  2728.   for (i = 0; i < bufp->used; i++)
  2729.     printchar (bufp->buffer[i]);
  2730.   
  2731.   printf ("\n%d allocated, %d used.\n", bufp->allocated, bufp->used);
  2732.   
  2733.   printf ("Allowed by fastmap: ");
  2734.   for (i = 0; i < (1 << BYTEWIDTH); i++)
  2735.     if (bufp->fastmap[i])
  2736.       printchar (i);
  2737.   printf ("\nAllowed by translate: ");
  2738.   if (bufp->translate)
  2739.     for (i = 0; i < (1 << BYTEWIDTH); i++)
  2740.       if (bufp->translate[i])
  2741.     printchar (i);
  2742.   printf ("\nfastmap is%s accurate\n", bufp->fastmap_accurate ? "" : "n't");
  2743.   printf ("can %s be null\n----------", bufp->can_be_null ? "" : "not");
  2744. }
  2745. #endif /* NOTDEF */
  2746.  
  2747. printchar (c)
  2748.      char c;
  2749. {
  2750.   if (c < 040 || c >= 0177)
  2751.     {
  2752.       putchar ('\\');
  2753.       putchar (((c >> 6) & 3) + '0');
  2754.       putchar (((c >> 3) & 7) + '0');
  2755.       putchar ((c & 7) + '0');
  2756.     }
  2757.   else
  2758.     putchar (c);
  2759. }
  2760.  
  2761. error (string)
  2762.      char *string;
  2763. {
  2764.   puts (string);
  2765.   exit (1);
  2766. }
  2767. #endif /* test */
  2768.